From 5bf6621e2e2982500fbf943c65d2bd01dadea11a Mon Sep 17 00:00:00 2001 From: Yuval Levental Date: Thu, 4 Dec 2025 13:40:19 +0000 Subject: [PATCH 1/2] Add C++ unordered-set empty() term entry --- .../unordered-set/terms/empty/empty.md | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 content/cpp/concepts/unordered-set/terms/empty/empty.md diff --git a/content/cpp/concepts/unordered-set/terms/empty/empty.md b/content/cpp/concepts/unordered-set/terms/empty/empty.md new file mode 100644 index 00000000000..0b0b573b40d --- /dev/null +++ b/content/cpp/concepts/unordered-set/terms/empty/empty.md @@ -0,0 +1,97 @@ +--- +Title: '.empty()' +Description: 'Checks whether the unordered set is empty.' +Subjects: + - 'Computer Science' + - 'Game Development' +Tags: + - 'Containers' + - 'Functions' + - 'Sets' + - 'STL' +CatalogContent: + - 'learn-c-plus-plus' + - 'paths/computer-science' +--- + +The **`.empty()`** method checks whether an [`unordered_set`](https://www.codecademy.com/resources/docs/cpp/unordered-set) container has no elements. It returns `true` if the container is empty (i.e., its size is 0) and `false` otherwise. + +## Syntax + +```pseudo +unordered_set_name.empty() +``` + +- `unordered_set_name`: The name of the `unordered_set` being checked for emptiness. + +**Parameters:** + +This method does not take any parameters. + +**Return Value:** + +Returns `true` if the `unordered_set` is empty and `false` otherwise. + +## Example + +The following example demonstrates how to use the `.empty()` method with `std::unordered_set` in C++: + +```cpp +#include +#include + +int main() { + std::unordered_set numbers; + + if (numbers.empty()) { + std::cout << "Unordered set is empty\n"; + } else { + std::cout << "Unordered set has elements\n"; + } + + numbers.insert(10); + numbers.insert(20); + numbers.insert(30); + + if (numbers.empty()) { + std::cout << "Unordered set is empty\n"; + } else { + std::cout << "Unordered set has elements\n"; + } + + return 0; +} +``` + +The output of the above code is: + +```shell +Unordered set is empty +Unordered set has elements +``` + +## Codebyte Example + +In this example, the `.empty()` method is used to control a loop that processes and removes elements from an `unordered_set` until it becomes empty: + +```codebyte/cpp +#include +#include + +int main() { + std::unordered_set numbers = {5, 10, 15, 20, 25}; + + std::cout << "Processing elements: "; + + while (!numbers.empty()) { + auto it = numbers.begin(); + std::cout << *it << " "; + numbers.erase(it); + } + + std::cout << "\n"; + std::cout << "Unordered set is now empty: " << std::boolalpha << numbers.empty() << "\n"; + + return 0; +} +``` From 0fc87e1ef8eb03bf0f1aa72fd0a427a8e2c5c9ce Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Fri, 5 Dec 2025 13:09:19 +0530 Subject: [PATCH 2/2] minor content fixes