Skip to content
This repository was archived by the owner on Feb 27, 2026. It is now read-only.

Commit 12028a0

Browse files
authored
[Term Entry] C++ Unordered-sets: empty()
* Add C++ unordered-set empty() term entry * minor content fixes * Update empty.md ---------
1 parent 32412d8 commit 12028a0

File tree

1 file changed

+95
-0
lines changed
  • content/cpp/concepts/unordered-set/terms/empty

1 file changed

+95
-0
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
---
2+
Title: '.empty()'
3+
Description: 'Checks whether the unordered set is empty.'
4+
Subjects:
5+
- 'Computer Science'
6+
- 'Game Development'
7+
Tags:
8+
- 'Containers'
9+
- 'Functions'
10+
- 'Sets'
11+
- 'STL'
12+
CatalogContent:
13+
- 'learn-c-plus-plus'
14+
- 'paths/computer-science'
15+
---
16+
17+
The **`.empty()`** method checks whether an `unordered_set` has no elements. It returns `true` if the container is empty (i.e., its size is 0) and `false` otherwise.
18+
19+
## Syntax
20+
21+
```pseudo
22+
unordered_set_name.empty()
23+
```
24+
25+
**Parameters:**
26+
27+
This method does not take any parameters.
28+
29+
**Return value:**
30+
31+
Returns `true` if the `unordered_set` is empty and `false` otherwise.
32+
33+
## Example
34+
35+
The following example demonstrates how to use the `.empty()` method with `std::unordered_set` in C++:
36+
37+
```cpp
38+
#include <iostream>
39+
#include <unordered_set>
40+
41+
int main() {
42+
std::unordered_set<int> numbers;
43+
44+
if (numbers.empty()) {
45+
std::cout << "Unordered set is empty\n";
46+
} else {
47+
std::cout << "Unordered set has elements\n";
48+
}
49+
50+
numbers.insert(10);
51+
numbers.insert(20);
52+
numbers.insert(30);
53+
54+
if (numbers.empty()) {
55+
std::cout << "Unordered set is empty\n";
56+
} else {
57+
std::cout << "Unordered set has elements\n";
58+
}
59+
60+
return 0;
61+
}
62+
```
63+
64+
The output of the above code is:
65+
66+
```shell
67+
Unordered set is empty
68+
Unordered set has elements
69+
```
70+
71+
## Codebyte Example
72+
73+
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:
74+
75+
```codebyte/cpp
76+
#include <iostream>
77+
#include <unordered_set>
78+
79+
int main() {
80+
std::unordered_set<int> numbers = {5, 10, 15, 20, 25};
81+
82+
std::cout << "Processing elements: ";
83+
84+
while (!numbers.empty()) {
85+
auto it = numbers.begin();
86+
std::cout << *it << " ";
87+
numbers.erase(it);
88+
}
89+
90+
std::cout << "\n";
91+
std::cout << "Unordered set is now empty? - " << std::boolalpha << numbers.empty() << "\n";
92+
93+
return 0;
94+
}
95+
```

0 commit comments

Comments
 (0)