Skip to content

Commit 86be392

Browse files
committed
feat(cpp): add empty() for unordered_set
Closes #8048
1 parent 6dfcf1f commit 86be392

File tree

1 file changed

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

1 file changed

+73
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
---
2+
Title: C++ Unordered Set empty()
3+
Description: The C++ `empty()` method, when used with an unordered_set, checks if the unordered_set container is empty or not.
4+
---
5+
6+
The C++ `empty()` method, when used with an unordered_set, checks if the unordered_set container is empty or not.
7+
8+
## Syntax
9+
10+
```cpp
11+
unordered_set_name.empty()
12+
```
13+
14+
The `empty()` method does not take any parameters and returns a boolean value:
15+
16+
- `true`: if the unordered_set is empty.
17+
- `false`: if the unordered_set is not empty.
18+
19+
## Example
20+
21+
```cpp
22+
#include <iostream>
23+
#include <unordered_set>
24+
#include <string>
25+
26+
int main() {
27+
std::unordered_set<std::string> groceries;
28+
29+
if (groceries.empty()) {
30+
std::cout << "The grocery list is empty!\n";
31+
} else {
32+
std::cout << "The grocery list is not empty.\n";
33+
}
34+
35+
groceries.insert("Milk");
36+
groceries.insert("Eggs");
37+
38+
if (groceries.empty()) {
39+
std::cout << "The grocery list is empty!\n";
40+
} else {
41+
std::cout << "The grocery list is not empty.\n";
42+
}
43+
44+
return 0;
45+
}
46+
```
47+
48+
## Codebyte Example
49+
50+
```codebyte/cpp
51+
#include <iostream>
52+
#include <unordered_set>
53+
54+
int main() {
55+
std::unordered_set<int> my_set;
56+
57+
if (my_set.empty()) {
58+
std::cout << "The unordered_set is empty.\n";
59+
} else {
60+
std::cout << "The unordered_set is not empty.\n";
61+
}
62+
63+
my_set.insert(10);
64+
65+
if (my_set.empty()) {
66+
std::cout << "The unordered_set is empty.\n";
67+
} else {
68+
std::cout << "The unordered_set is not empty.\n";
69+
}
70+
71+
return 0;
72+
}
73+
```

0 commit comments

Comments
 (0)