diff --git a/content/cpp/concepts/unordered-set/terms/size/size.md b/content/cpp/concepts/unordered-set/terms/size/size.md new file mode 100644 index 00000000000..6b5400518be --- /dev/null +++ b/content/cpp/concepts/unordered-set/terms/size/size.md @@ -0,0 +1,113 @@ +--- +Title: 'size()' +Description: 'Returns the number of elements currently stored in the unordered set.' +Subjects: + - 'Code Foundations' + - 'Computer Science' +Tags: + - 'Containers' + - 'Methods' + - 'Sets' + - 'STL' +CatalogContent: + - 'learn-c-plus-plus' + - 'paths/computer-science' +--- + +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. + +## Syntax + +```pseudo +set_name.size(); +``` + +**Parameters:** + +This function takes no parameters. + +**Return value:** + +Returns a `size_type` value representing the number of elements in the `unordered_set`. + +## Example 1: Basic Usage of `size()` + +In this example the program inserts one element into an `unordered_set` and prints its size: + +```cpp +#include +#include +using namespace std; + +int main() { + unordered_set mySet; + mySet.insert(10); + + cout << "Size: " << mySet.size(); + return 0; +} +``` + +The output of this code is: + +```shell +Size: 1 +``` + +This returns 1, because the unordered_set only has 1 element. + +## Example 2: Counting unique elements + +In this example the program initializes an `unordered_set` with duplicates, prints its size, and displays the unique elements: + +```cpp +#include +#include +using namespace std; + +int main() { + unordered_set mySet {1, 2, 3, 3, 1, 3, 2, 4, 5, 7}; + + cout << "There are " << mySet.size() << " elements.\n"; + cout << "The elements are: "; + for (int ele : mySet) { + cout << ele << " "; + } + return 0; +} +``` + +The output of this code is: + +```shell +There are 6 elements. +The elements are: 7 5 4 3 2 1 +``` + +> **Note:** The order of elements may vary. + +This returns 6, because an `unordered_set` cannot contain duplicates. The unique elements are: 7, 5, 4, 3, 2, 1 + +## Codebyte example + +In this example the program compares `size()` with `sizeof()` to show that element count and memory footprint are unrelated: + +```codebyte/cpp +#include +#include +using namespace std; + +int main() { + unordered_set mySet; + + for (int i = 0; i < 10; i++) { + mySet.insert(i); + } + + cout << "Number of elements: " << mySet.size() << "\n"; + cout << "The set's byte usage: " << sizeof(mySet); + return 0; +} +``` + +`size()` returns the number of stored elements, while `sizeof()` returns the memory footprint of the container object, which does not grow with element count.