Skip to content

Commit 3ebfb5e

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

File tree

1 file changed

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

1 file changed

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

0 commit comments

Comments
 (0)