Skip to content

Commit 6ae9d6b

Browse files
add size.md
1 parent 95f0e9a commit 6ae9d6b

File tree

1 file changed

+105
-0
lines changed
  • content/cpp/concepts/unordered-set/terms/size

1 file changed

+105
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
---
2+
Title: 'size()'
3+
Description: 'size() returns the number of elements in a set as an int'
4+
Subjects:
5+
- 'Code Foundations'
6+
- 'Computer Science'
7+
Tags:
8+
- 'Memory'
9+
- 'Parameters'
10+
- 'Script'
11+
- 'Syntax'
12+
CatalogContent:
13+
- 'learn-c-plus-plus'
14+
- 'paths/computer-science'
15+
---
16+
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.
19+
20+
21+
<!-- ##Syntax section that includes the syntax of the method and its details -->
22+
## Syntax
23+
### How to use size
24+
```cpp
25+
#include <unordered_set>
26+
unordered_set<dataType> set_name;
27+
set_name.size();
28+
```
29+
```size()``` is in the ```<unordered_set>``` header file, which needs to be included for the member function to work.
30+
31+
**Parameters**
32+
33+
This function does not take any parameters.
34+
35+
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
39+
```cpp
40+
#include <iostream>
41+
// Removes the need for std
42+
using namespace std;
43+
#include <unordered_set>
44+
45+
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;
53+
}
54+
```
55+
This returns 1, because the unordered_set only has 1 element.
56+
57+
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+
#include <iostream>
62+
// Removes the need for std
63+
using namespace std;
64+
#include <unordered_set>
65+
66+
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
72+
cout << "There are " << mySet.size() << " elements.\n";
73+
cout << "The elements are: ";
74+
for(int ele : mySet) {
75+
cout << ele << " ";
76+
}
77+
return 0;
78+
}
79+
```
80+
This returns 6, because an unordered_set cannot contain duplicates. The unique elements are: 7, 5, 4, 3, 2, 1
81+
82+
83+
<!-- An ## Codebyte section that have a compilable code inside it showing the current entry in use. Use ```codebyte/cpp to add compilable code. -->
84+
## 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.
87+
88+
```codebyte/cpp
89+
#include <iostream>
90+
// Removes the need for std
91+
using namespace std;
92+
#include <unordered_set>
93+
94+
int main() {
95+
// Initializes an unordered_set
96+
unordered_set<int> mySet;
97+
// Adds elements to the unordered_set
98+
for (int i = 0; i < 10; i++) {
99+
mySet.insert(i);
100+
}
101+
cout << "Number of elements: " << mySet.size() << "\n";
102+
cout << "The set's byte usage: " << sizeof(mySet);
103+
return 0;
104+
}
105+
```

0 commit comments

Comments
 (0)