Skip to content

Commit 273a9f4

Browse files
Revise size() documentation and examples
Updated the description and examples for the size() function in unordered_set. Enhanced clarity and corrected syntax in code examples.
1 parent 6ae9d6b commit 273a9f4

File tree

1 file changed

+59
-51
lines changed
  • content/cpp/concepts/unordered-set/terms/size

1 file changed

+59
-51
lines changed
Lines changed: 59 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,113 @@
11
---
22
Title: 'size()'
3-
Description: 'size() returns the number of elements in a set as an int'
3+
Description: 'Returns the number of elements currently stored in the unordered set.'
44
Subjects:
55
- 'Code Foundations'
66
- 'Computer Science'
77
Tags:
8-
- 'Memory'
9-
- 'Parameters'
10-
- 'Script'
11-
- 'Syntax'
8+
- 'Containers'
9+
- 'Methods'
10+
- 'Sets'
11+
- 'STL'
1212
CatalogContent:
1313
- 'learn-c-plus-plus'
1414
- 'paths/computer-science'
1515
---
1616

17-
<!-- A description of the term -->
18-
The member function ```size()``` in ```<unordered_set>``` returns the number of elements currently stored in an unordered_set as an ```int```. If the unordered_set is empty, it returns 0.
17+
The **`size()`** member function of `unordered_set` returns the number of elements currently stored in the container as a `size_type`. If the `unordered_set` is empty, it returns 0.
1918

20-
21-
<!-- ##Syntax section that includes the syntax of the method and its details -->
2219
## Syntax
23-
### How to use size
24-
```cpp
25-
#include <unordered_set>
26-
unordered_set<dataType> set_name;
20+
21+
```pseudo
2722
set_name.size();
2823
```
29-
```size()``` is in the ```<unordered_set>``` header file, which needs to be included for the member function to work.
3024

31-
**Parameters**
25+
**Parameters:**
3226

33-
This function does not take any parameters.
27+
This function takes no parameters.
3428

29+
**Return value:**
30+
31+
Returns a `size_type` value representing the number of elements in the `unordered_set`.
32+
33+
## Example 1: Basic Usage of `size()`
34+
35+
In this example the program inserts one element into an `unordered_set` and prints its size:
3536

36-
<!-- An ##Example section that show an example of the current entry. -->
37-
## Example: Pattern
38-
This example shows how to setup an unordered_set
3937
```cpp
4038
#include <iostream>
41-
// Removes the need for std
42-
using namespace std;
4339
#include <unordered_set>
40+
using namespace std;
4441

4542
int main() {
46-
// Initializes an unordered_set
47-
unordered_set<int> mySet;
48-
// Inserts 10 into the unordered_set
49-
mySet.insert(10);
50-
// Prints the size of the unordered_set
51-
cout << "Size: " << mySet.size();
52-
return 0;
43+
unordered_set<int> mySet;
44+
mySet.insert(10);
45+
46+
cout << "Size: " << mySet.size();
47+
return 0;
5348
}
5449
```
50+
51+
The output of this code is:
52+
53+
```shell
54+
Size: 1
55+
```
56+
5557
This returns 1, because the unordered_set only has 1 element.
5658

59+
## Example 2: Counting unique elements
5760

58-
## Example: How many elements are unique
59-
This example gives the size of the unordered_set and prints each element out.
60-
``` cpp
61+
In this example the program initializes an `unordered_set` with duplicates, prints its size, and displays the unique elements:
62+
63+
```cpp
6164
#include <iostream>
62-
// Removes the need for std
63-
using namespace std;
6465
#include <unordered_set>
66+
using namespace std;
6567

6668
int main() {
67-
// Declares an unordered_set with duplicates
68-
unordered_set<int> mySet {
69-
1, 2, 3, 3, 1, 3, 2, 4, 5, 7
70-
};
71-
// Prints the size of the unordered_set
69+
unordered_set<int> mySet {1, 2, 3, 3, 1, 3, 2, 4, 5, 7};
70+
7271
cout << "There are " << mySet.size() << " elements.\n";
73-
cout << "The elements are: ";
74-
for(int ele : mySet) {
75-
cout << ele << " ";
76-
}
72+
cout << "The elements are: ";
73+
for (int ele : mySet) {
74+
cout << ele << " ";
75+
}
7776
return 0;
7877
}
7978
```
80-
This returns 6, because an unordered_set cannot contain duplicates. The unique elements are: 7, 5, 4, 3, 2, 1
8179

80+
The output of this code is:
81+
82+
```shell
83+
There are 6 elements.
84+
The elements are: 7 5 4 3 2 1
85+
```
86+
87+
> **Note:** The order of elements may vary.
88+
89+
This returns 6, because an `unordered_set` cannot contain duplicates. The unique elements are: 7, 5, 4, 3, 2, 1
8290

83-
<!-- An ## Codebyte section that have a compilable code inside it showing the current entry in use. Use ```codebyte/cpp to add compilable code. -->
8491
## Codebyte example
85-
The following code shows the distinction between ```size()``` and ```sizeof()```: ```size()``` gives the total number of elements in the unordered_set, while ```sizeof()``` gives the total memory in bytes of the unordered_set.
86-
Try and change the number of iterations.
92+
93+
In this example the program compares `size()` with `sizeof()` to show that element count and memory footprint are unrelated:
8794

8895
```codebyte/cpp
8996
#include <iostream>
90-
// Removes the need for std
91-
using namespace std;
9297
#include <unordered_set>
98+
using namespace std;
9399
94100
int main() {
95-
// Initializes an unordered_set
96101
unordered_set<int> mySet;
97-
// Adds elements to the unordered_set
102+
98103
for (int i = 0; i < 10; i++) {
99-
mySet.insert(i);
104+
mySet.insert(i);
100105
}
106+
101107
cout << "Number of elements: " << mySet.size() << "\n";
102108
cout << "The set's byte usage: " << sizeof(mySet);
103109
return 0;
104110
}
105111
```
112+
113+
`size()` returns the number of stored elements, while `sizeof()` returns the memory footprint of the container object, which does not grow with element count.

0 commit comments

Comments
 (0)