From 6ae9d6b374fdf89484408ebbbf4a9f4f39003edd Mon Sep 17 00:00:00 2001 From: KBK <231205490+KristofferBarnerKleis@users.noreply.github.com> Date: Thu, 4 Dec 2025 13:37:50 +0100 Subject: [PATCH 1/2] add size.md --- .../concepts/unordered-set/terms/size/size.md | 105 ++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 content/cpp/concepts/unordered-set/terms/size/size.md 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..80fe48b35d9 --- /dev/null +++ b/content/cpp/concepts/unordered-set/terms/size/size.md @@ -0,0 +1,105 @@ +--- +Title: 'size()' +Description: 'size() returns the number of elements in a set as an int' +Subjects: + - 'Code Foundations' + - 'Computer Science' +Tags: + - 'Memory' + - 'Parameters' + - 'Script' + - 'Syntax' +CatalogContent: + - 'learn-c-plus-plus' + - 'paths/computer-science' +--- + + +The member function ```size()``` in `````` returns the number of elements currently stored in an unordered_set as an ```int```. If the unordered_set is empty, it returns 0. + + + +## Syntax +### How to use size +```cpp +#include +unordered_set set_name; +set_name.size(); +``` +```size()``` is in the `````` header file, which needs to be included for the member function to work. + +**Parameters** + +This function does not take any parameters. + + + +## Example: Pattern +This example shows how to setup an unordered_set +```cpp +#include +// Removes the need for std +using namespace std; +#include + +int main() { + // Initializes an unordered_set + unordered_set mySet; + // Inserts 10 into the unordered_set + mySet.insert(10); + // Prints the size of the unordered_set + cout << "Size: " << mySet.size(); + return 0; +} +``` +This returns 1, because the unordered_set only has 1 element. + + +## Example: How many elements are unique +This example gives the size of the unordered_set and prints each element out. +``` cpp +#include +// Removes the need for std +using namespace std; +#include + +int main() { + // Declares an unordered_set with duplicates + unordered_set mySet { + 1, 2, 3, 3, 1, 3, 2, 4, 5, 7 + }; + // Prints the size of the unordered_set + cout << "There are " << mySet.size() << " elements.\n"; + cout << "The elements are: "; + for(int ele : mySet) { + cout << ele << " "; + } + return 0; +} +``` +This returns 6, because an unordered_set cannot contain duplicates. The unique elements are: 7, 5, 4, 3, 2, 1 + + + +## Codebyte example +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. +Try and change the number of iterations. + +```codebyte/cpp +#include +// Removes the need for std +using namespace std; +#include + +int main() { + // Initializes an unordered_set + unordered_set mySet; + // Adds elements to the unordered_set + 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; +} +``` From 273a9f428d855f529b5ef2da5ab846c0d72e6167 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Fri, 5 Dec 2025 15:22:07 +0530 Subject: [PATCH 2/2] 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. --- .../concepts/unordered-set/terms/size/size.md | 110 ++++++++++-------- 1 file changed, 59 insertions(+), 51 deletions(-) diff --git a/content/cpp/concepts/unordered-set/terms/size/size.md b/content/cpp/concepts/unordered-set/terms/size/size.md index 80fe48b35d9..6b5400518be 100644 --- a/content/cpp/concepts/unordered-set/terms/size/size.md +++ b/content/cpp/concepts/unordered-set/terms/size/size.md @@ -1,105 +1,113 @@ --- Title: 'size()' -Description: 'size() returns the number of elements in a set as an int' +Description: 'Returns the number of elements currently stored in the unordered set.' Subjects: - 'Code Foundations' - 'Computer Science' Tags: - - 'Memory' - - 'Parameters' - - 'Script' - - 'Syntax' + - 'Containers' + - 'Methods' + - 'Sets' + - 'STL' CatalogContent: - 'learn-c-plus-plus' - 'paths/computer-science' --- - -The member function ```size()``` in `````` returns the number of elements currently stored in an unordered_set as an ```int```. If the unordered_set is empty, it returns 0. +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 -### How to use size -```cpp -#include -unordered_set set_name; + +```pseudo set_name.size(); ``` -```size()``` is in the `````` header file, which needs to be included for the member function to work. -**Parameters** +**Parameters:** -This function does not take any 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: - -## Example: Pattern -This example shows how to setup an unordered_set ```cpp #include -// Removes the need for std -using namespace std; #include +using namespace std; int main() { - // Initializes an unordered_set - unordered_set mySet; - // Inserts 10 into the unordered_set - mySet.insert(10); - // Prints the size of the unordered_set - cout << "Size: " << mySet.size(); - return 0; + 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 -## Example: How many elements are unique -This example gives the size of the unordered_set and prints each element out. -``` cpp +In this example the program initializes an `unordered_set` with duplicates, prints its size, and displays the unique elements: + +```cpp #include -// Removes the need for std -using namespace std; #include +using namespace std; int main() { - // Declares an unordered_set with duplicates - unordered_set mySet { - 1, 2, 3, 3, 1, 3, 2, 4, 5, 7 - }; - // Prints the size of the unordered_set + 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 << " "; - } + cout << "The elements are: "; + for (int ele : mySet) { + cout << ele << " "; + } return 0; } ``` -This returns 6, because an unordered_set cannot contain duplicates. The unique elements are: 7, 5, 4, 3, 2, 1 +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 -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. -Try and change the number of iterations. + +In this example the program compares `size()` with `sizeof()` to show that element count and memory footprint are unrelated: ```codebyte/cpp #include -// Removes the need for std -using namespace std; #include +using namespace std; int main() { - // Initializes an unordered_set unordered_set mySet; - // Adds elements to the unordered_set + for (int i = 0; i < 10; i++) { - mySet.insert(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.