|
| 1 | +--- |
| 2 | +Title: 'size()' |
| 3 | +Description: 'Returns the number of elements currently stored in the unordered set.' |
| 4 | +Subjects: |
| 5 | + - 'Code Foundations' |
| 6 | + - 'Computer Science' |
| 7 | +Tags: |
| 8 | + - 'Containers' |
| 9 | + - 'Methods' |
| 10 | + - 'Sets' |
| 11 | + - 'STL' |
| 12 | +CatalogContent: |
| 13 | + - 'learn-c-plus-plus' |
| 14 | + - 'paths/computer-science' |
| 15 | +--- |
| 16 | + |
| 17 | +The **`size()`** member [function](https://www.codecademy.com/resources/docs/cpp/functions) of `unordered_set` is used to return the number of elements currently stored in the container as a `size_type`. If the `unordered_set` is empty, it returns `0`. |
| 18 | + |
| 19 | +## Syntax |
| 20 | + |
| 21 | +```pseudo |
| 22 | +set_name.size(); |
| 23 | +``` |
| 24 | + |
| 25 | +**Parameters:** |
| 26 | + |
| 27 | +This function takes no parameters. |
| 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: |
| 36 | + |
| 37 | +```cpp |
| 38 | +#include <iostream> |
| 39 | +#include <unordered_set> |
| 40 | +using namespace std; |
| 41 | + |
| 42 | +int main() { |
| 43 | + unordered_set<int> mySet; |
| 44 | + mySet.insert(10); |
| 45 | + |
| 46 | + cout << "Size: " << mySet.size(); |
| 47 | + return 0; |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +The output of this code is: |
| 52 | + |
| 53 | +```shell |
| 54 | +Size: 1 |
| 55 | +``` |
| 56 | + |
| 57 | +## Example 2: Counting Unique Elements |
| 58 | + |
| 59 | +In this example, the program initializes an `unordered_set` with integers (including duplicates), prints its size, and displays its elements: |
| 60 | + |
| 61 | +```cpp |
| 62 | +#include <iostream> |
| 63 | +#include <unordered_set> |
| 64 | +using namespace std; |
| 65 | + |
| 66 | +int main() { |
| 67 | + unordered_set<int> mySet {1, 2, 3, 3, 1, 3, 2, 4, 5, 7}; |
| 68 | + |
| 69 | + cout << "There are " << mySet.size() << " elements.\n"; |
| 70 | + cout << "The elements are: "; |
| 71 | + for (int ele : mySet) { |
| 72 | + cout << ele << " "; |
| 73 | + } |
| 74 | + return 0; |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +The output of this code is: |
| 79 | + |
| 80 | +```shell |
| 81 | +There are 6 elements. |
| 82 | +The elements are: 7 5 4 3 2 1 |
| 83 | +``` |
| 84 | + |
| 85 | +> **Note:** The order of elements may vary. |
| 86 | +
|
| 87 | +Since an `unordered_set` cannot contain duplicates, the code returns `6` as its size and only the unique elements in the container. |
| 88 | + |
| 89 | +## Codebyte Example |
| 90 | + |
| 91 | +In this example, the program compares `size()` with `sizeof()` to show that element count and memory footprint are unrelated: |
| 92 | + |
| 93 | +```codebyte/cpp |
| 94 | +#include <iostream> |
| 95 | +#include <unordered_set> |
| 96 | +using namespace std; |
| 97 | +
|
| 98 | +int main() { |
| 99 | + unordered_set<int> mySet; |
| 100 | +
|
| 101 | + for (int i = 0; i < 10; i++) { |
| 102 | + mySet.insert(i); |
| 103 | + } |
| 104 | +
|
| 105 | + cout << "Number of elements: " << mySet.size() << "\n"; |
| 106 | + cout << "The set's byte usage: " << sizeof(mySet); |
| 107 | + return 0; |
| 108 | +} |
| 109 | +``` |
| 110 | + |
| 111 | +`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