|
1 | 1 | --- |
2 | 2 | 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.' |
4 | 4 | Subjects: |
5 | 5 | - 'Code Foundations' |
6 | 6 | - 'Computer Science' |
7 | 7 | Tags: |
8 | | - - 'Memory' |
9 | | - - 'Parameters' |
10 | | - - 'Script' |
11 | | - - 'Syntax' |
| 8 | + - 'Containers' |
| 9 | + - 'Methods' |
| 10 | + - 'Sets' |
| 11 | + - 'STL' |
12 | 12 | CatalogContent: |
13 | 13 | - 'learn-c-plus-plus' |
14 | 14 | - 'paths/computer-science' |
15 | 15 | --- |
16 | 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. |
| 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. |
19 | 18 |
|
20 | | - |
21 | | -<!-- ##Syntax section that includes the syntax of the method and its details --> |
22 | 19 | ## Syntax |
23 | | -### How to use size |
24 | | -```cpp |
25 | | -#include <unordered_set> |
26 | | -unordered_set<dataType> set_name; |
| 20 | + |
| 21 | +```pseudo |
27 | 22 | set_name.size(); |
28 | 23 | ``` |
29 | | -```size()``` is in the ```<unordered_set>``` header file, which needs to be included for the member function to work. |
30 | 24 |
|
31 | | -**Parameters** |
| 25 | +**Parameters:** |
32 | 26 |
|
33 | | -This function does not take any parameters. |
| 27 | +This function takes no parameters. |
34 | 28 |
|
| 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: |
35 | 36 |
|
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 | 37 | ```cpp |
40 | 38 | #include <iostream> |
41 | | -// Removes the need for std |
42 | | -using namespace std; |
43 | 39 | #include <unordered_set> |
| 40 | +using namespace std; |
44 | 41 |
|
45 | 42 | 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; |
53 | 48 | } |
54 | 49 | ``` |
| 50 | + |
| 51 | +The output of this code is: |
| 52 | + |
| 53 | +```shell |
| 54 | +Size: 1 |
| 55 | +``` |
| 56 | + |
55 | 57 | This returns 1, because the unordered_set only has 1 element. |
56 | 58 |
|
| 59 | +## Example 2: Counting unique elements |
57 | 60 |
|
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 |
61 | 64 | #include <iostream> |
62 | | -// Removes the need for std |
63 | | -using namespace std; |
64 | 65 | #include <unordered_set> |
| 66 | +using namespace std; |
65 | 67 |
|
66 | 68 | 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 | + |
72 | 71 | 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 | + } |
77 | 76 | return 0; |
78 | 77 | } |
79 | 78 | ``` |
80 | | -This returns 6, because an unordered_set cannot contain duplicates. The unique elements are: 7, 5, 4, 3, 2, 1 |
81 | 79 |
|
| 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 |
82 | 90 |
|
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 | 91 | ## 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: |
87 | 94 |
|
88 | 95 | ```codebyte/cpp |
89 | 96 | #include <iostream> |
90 | | -// Removes the need for std |
91 | | -using namespace std; |
92 | 97 | #include <unordered_set> |
| 98 | +using namespace std; |
93 | 99 |
|
94 | 100 | int main() { |
95 | | - // Initializes an unordered_set |
96 | 101 | unordered_set<int> mySet; |
97 | | - // Adds elements to the unordered_set |
| 102 | +
|
98 | 103 | for (int i = 0; i < 10; i++) { |
99 | | - mySet.insert(i); |
| 104 | + mySet.insert(i); |
100 | 105 | } |
| 106 | +
|
101 | 107 | cout << "Number of elements: " << mySet.size() << "\n"; |
102 | 108 | cout << "The set's byte usage: " << sizeof(mySet); |
103 | 109 | return 0; |
104 | 110 | } |
105 | 111 | ``` |
| 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