Skip to content

Commit 7c05408

Browse files
minor content fixes
1 parent 056c520 commit 7c05408

File tree

1 file changed

+23
-28
lines changed
  • content/cpp/concepts/unordered-set/terms/count

1 file changed

+23
-28
lines changed
Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,42 @@
11
---
2-
Title: '.count()'
2+
Title: 'count()'
33
Description: 'Returns the number of elements with a specific key in an unordered_set.'
44
Subjects:
5+
- 'Code Foundations'
56
- 'Computer Science'
6-
- 'Programming'
77
Tags:
8-
- 'C++'
9-
- 'Unordered Set'
8+
- 'Methods'
9+
- 'Sets'
1010
- 'STL'
11-
- 'Searching'
1211
CatalogContent:
1312
- 'learn-c-plus-plus'
1413
- 'paths/computer-science'
1514
---
1615

17-
The **`.count()`** method is used to determine if a specific element (key) is present within a C++ `std::unordered_set`.
16+
The **`count()`** method checks whether a given key exists in a `std::unordered_set`. Since this container stores only unique elements, `count()` will always return either:
1817

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.
18+
1. `1`: If the element is found in the set.
19+
2. `0`: If the element is not found in the set.
2320

2421
This method is commonly used as a fast, O(1) average time complexity way to check for element existence.
2522

2623
## Syntax
2724

28-
The `.count()` method takes one argument: the value (key) to search for.
29-
30-
```cpp
25+
```pseudo
3126
unordered_set_name.count(key);
3227
```
3328

34-
## Parameters
29+
**Parameters:**
3530

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`.
31+
- `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`.
3732

38-
## Return Value
33+
**Return value:**
3934

40-
Returns an integer (`1` if the element exists, `0` otherwise).
35+
Returns an integer. `1` if the element exists, `0` otherwise.
4136

4237
## Example
4338

44-
This example demonstrates using `.count()` to check for the presence of elements within a set of strings.
39+
This example demonstrates using `count()` to check for the presence of elements within a set of strings:
4540

4641
```cpp
4742
#include <iostream>
@@ -74,9 +69,9 @@ int main() {
7469
}
7570
```
7671

77-
Output:
72+
The output of the code is:
7873

79-
```
74+
```shell
8075
Inventory contains:
8176
- Potion
8277
- Shield
@@ -86,28 +81,28 @@ Inventory contains:
8681
'Axe' is not present (Count: 0).
8782
```
8883

89-
## Codebyte
84+
## Codebyte Example
9085

91-
Use the Codebyte below to check for the presence of an item in a set of integers.
86+
Use the Codebyte below to check for the presence of an item in a set of integers:
9287

93-
```cpp
88+
```codebyte/cpp
9489
#include <iostream>
9590
#include <unordered_set>
9691
9792
int main() {
9893
std::unordered_set<int> unique_ids = {101, 205, 330};
99-
94+
10095
int search_key = 205;
10196
int missing_key = 400;
10297
10398
// Check the count for the element 205
104-
std::cout << "Count for " << search_key << ": "
99+
std::cout << "Count for " << search_key << ": "
105100
<< unique_ids.count(search_key) << "\n";
106-
101+
107102
// Check the count for the element 400
108-
std::cout << "Count for " << missing_key << ": "
103+
std::cout << "Count for " << missing_key << ": "
109104
<< unique_ids.count(missing_key) << "\n";
110105
111106
return 0;
112107
}
113-
```
108+
```

0 commit comments

Comments
 (0)