Skip to content

Commit 056c520

Browse files
committed
docs: Add C++ unordered_set::count() term entry (#8027)
1 parent 8433d22 commit 056c520

File tree

1 file changed

+113
-0
lines changed
  • content/cpp/concepts/unordered-set/terms/count

1 file changed

+113
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
---
2+
Title: '.count()'
3+
Description: 'Returns the number of elements with a specific key in an unordered_set.'
4+
Subjects:
5+
- 'Computer Science'
6+
- 'Programming'
7+
Tags:
8+
- 'C++'
9+
- 'Unordered Set'
10+
- 'STL'
11+
- 'Searching'
12+
CatalogContent:
13+
- 'learn-c-plus-plus'
14+
- 'paths/computer-science'
15+
---
16+
17+
The **`.count()`** method is used to determine if a specific element (key) is present within a C++ `std::unordered_set`.
18+
19+
Because `std::unordered_set` only allows **unique** elements, the `.count()` method will only ever return one of two possible values:
20+
21+
1. **`1`**: If the element is found in the set.
22+
2. **`0`**: If the element is not found in the set.
23+
24+
This method is commonly used as a fast, O(1) average time complexity way to check for element existence.
25+
26+
## Syntax
27+
28+
The `.count()` method takes one argument: the value (key) to search for.
29+
30+
```cpp
31+
unordered_set_name.count(key);
32+
```
33+
34+
## Parameters
35+
36+
* `key` (const Key&): The value of the element to search for. Must be of the same type as the elements stored in the `unordered_set`.
37+
38+
## Return Value
39+
40+
Returns an integer (`1` if the element exists, `0` otherwise).
41+
42+
## Example
43+
44+
This example demonstrates using `.count()` to check for the presence of elements within a set of strings.
45+
46+
```cpp
47+
#include <iostream>
48+
#include <string>
49+
#include <unordered_set>
50+
51+
int main() {
52+
std::unordered_set<std::string> inventory = {
53+
"Sword",
54+
"Shield",
55+
"Potion"
56+
};
57+
58+
std::cout << "Inventory contains:\n";
59+
for (const auto& item : inventory) {
60+
std::cout << "- " << item << "\n";
61+
}
62+
63+
// 1. Check for an existing element
64+
if (inventory.count("Sword")) {
65+
std::cout << "\n'Sword' is present (Count: " << inventory.count("Sword") << ").\n";
66+
}
67+
68+
// 2. Check for a missing element
69+
if (inventory.count("Axe") == 0) {
70+
std::cout << "'Axe' is not present (Count: " << inventory.count("Axe") << ").\n";
71+
}
72+
73+
return 0;
74+
}
75+
```
76+
77+
Output:
78+
79+
```
80+
Inventory contains:
81+
- Potion
82+
- Shield
83+
- Sword
84+
85+
'Sword' is present (Count: 1).
86+
'Axe' is not present (Count: 0).
87+
```
88+
89+
## Codebyte
90+
91+
Use the Codebyte below to check for the presence of an item in a set of integers.
92+
93+
```cpp
94+
#include <iostream>
95+
#include <unordered_set>
96+
97+
int main() {
98+
std::unordered_set<int> unique_ids = {101, 205, 330};
99+
100+
int search_key = 205;
101+
int missing_key = 400;
102+
103+
// Check the count for the element 205
104+
std::cout << "Count for " << search_key << ": "
105+
<< unique_ids.count(search_key) << "\n";
106+
107+
// Check the count for the element 400
108+
std::cout << "Count for " << missing_key << ": "
109+
<< unique_ids.count(missing_key) << "\n";
110+
111+
return 0;
112+
}
113+
```

0 commit comments

Comments
 (0)