From 056c520f7bd4dd9b6153c644de6055023fd764b2 Mon Sep 17 00:00:00 2001 From: Rishabh Ranjan Singh Date: Tue, 25 Nov 2025 11:03:28 +0530 Subject: [PATCH 1/2] docs: Add C++ unordered_set::count() term entry (#8027) --- .../unordered-set/terms/count/count.md | 113 ++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 content/cpp/concepts/unordered-set/terms/count/count.md diff --git a/content/cpp/concepts/unordered-set/terms/count/count.md b/content/cpp/concepts/unordered-set/terms/count/count.md new file mode 100644 index 00000000000..f532d0b7feb --- /dev/null +++ b/content/cpp/concepts/unordered-set/terms/count/count.md @@ -0,0 +1,113 @@ +--- +Title: '.count()' +Description: 'Returns the number of elements with a specific key in an unordered_set.' +Subjects: + - 'Computer Science' + - 'Programming' +Tags: + - 'C++' + - 'Unordered Set' + - 'STL' + - 'Searching' +CatalogContent: + - 'learn-c-plus-plus' + - 'paths/computer-science' +--- + +The **`.count()`** method is used to determine if a specific element (key) is present within a C++ `std::unordered_set`. + +Because `std::unordered_set` only allows **unique** elements, the `.count()` method will only ever return one of two possible values: + +1. **`1`**: If the element is found in the set. +2. **`0`**: If the element is not found in the set. + +This method is commonly used as a fast, O(1) average time complexity way to check for element existence. + +## Syntax + +The `.count()` method takes one argument: the value (key) to search for. + +```cpp +unordered_set_name.count(key); +``` + +## Parameters + +* `key` (const Key&): The value of the element to search for. Must be of the same type as the elements stored in the `unordered_set`. + +## Return Value + +Returns an integer (`1` if the element exists, `0` otherwise). + +## Example + +This example demonstrates using `.count()` to check for the presence of elements within a set of strings. + +```cpp +#include +#include +#include + +int main() { + std::unordered_set inventory = { + "Sword", + "Shield", + "Potion" + }; + + std::cout << "Inventory contains:\n"; + for (const auto& item : inventory) { + std::cout << "- " << item << "\n"; + } + + // 1. Check for an existing element + if (inventory.count("Sword")) { + std::cout << "\n'Sword' is present (Count: " << inventory.count("Sword") << ").\n"; + } + + // 2. Check for a missing element + if (inventory.count("Axe") == 0) { + std::cout << "'Axe' is not present (Count: " << inventory.count("Axe") << ").\n"; + } + + return 0; +} +``` + +Output: + +``` +Inventory contains: +- Potion +- Shield +- Sword + +'Sword' is present (Count: 1). +'Axe' is not present (Count: 0). +``` + +## Codebyte + +Use the Codebyte below to check for the presence of an item in a set of integers. + +```cpp +#include +#include + +int main() { + std::unordered_set unique_ids = {101, 205, 330}; + + int search_key = 205; + int missing_key = 400; + + // Check the count for the element 205 + std::cout << "Count for " << search_key << ": " + << unique_ids.count(search_key) << "\n"; + + // Check the count for the element 400 + std::cout << "Count for " << missing_key << ": " + << unique_ids.count(missing_key) << "\n"; + + return 0; +} +``` \ No newline at end of file From 7c0540836e431f87b42993fa611e0aea85e178dc Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Wed, 26 Nov 2025 15:57:56 +0530 Subject: [PATCH 2/2] minor content fixes --- .../unordered-set/terms/count/count.md | 51 +++++++++---------- 1 file changed, 23 insertions(+), 28 deletions(-) diff --git a/content/cpp/concepts/unordered-set/terms/count/count.md b/content/cpp/concepts/unordered-set/terms/count/count.md index f532d0b7feb..839724329d5 100644 --- a/content/cpp/concepts/unordered-set/terms/count/count.md +++ b/content/cpp/concepts/unordered-set/terms/count/count.md @@ -1,47 +1,42 @@ --- -Title: '.count()' +Title: 'count()' Description: 'Returns the number of elements with a specific key in an unordered_set.' Subjects: + - 'Code Foundations' - 'Computer Science' - - 'Programming' Tags: - - 'C++' - - 'Unordered Set' + - 'Methods' + - 'Sets' - 'STL' - - 'Searching' CatalogContent: - 'learn-c-plus-plus' - 'paths/computer-science' --- -The **`.count()`** method is used to determine if a specific element (key) is present within a C++ `std::unordered_set`. +The **`count()`** method checks whether a given key exists in a `std::unordered_set`. Since this container stores only unique elements, `count()` will always return either: -Because `std::unordered_set` only allows **unique** elements, the `.count()` method will only ever return one of two possible values: - -1. **`1`**: If the element is found in the set. -2. **`0`**: If the element is not found in the set. +1. `1`: If the element is found in the set. +2. `0`: If the element is not found in the set. This method is commonly used as a fast, O(1) average time complexity way to check for element existence. ## Syntax -The `.count()` method takes one argument: the value (key) to search for. - -```cpp +```pseudo unordered_set_name.count(key); ``` -## Parameters +**Parameters:** -* `key` (const Key&): The value of the element to search for. Must be of the same type as the elements stored in the `unordered_set`. +- `key` (const Key&): The value of the element to search for. Must be of the same type as the elements stored in the `unordered_set`. -## Return Value +**Return value:** -Returns an integer (`1` if the element exists, `0` otherwise). +Returns an integer. `1` if the element exists, `0` otherwise. ## Example -This example demonstrates using `.count()` to check for the presence of elements within a set of strings. +This example demonstrates using `count()` to check for the presence of elements within a set of strings: ```cpp #include @@ -74,9 +69,9 @@ int main() { } ``` -Output: +The output of the code is: -``` +```shell Inventory contains: - Potion - Shield @@ -86,28 +81,28 @@ Inventory contains: 'Axe' is not present (Count: 0). ``` -## Codebyte +## Codebyte Example -Use the Codebyte below to check for the presence of an item in a set of integers. +Use the Codebyte below to check for the presence of an item in a set of integers: -```cpp +```codebyte/cpp #include #include int main() { std::unordered_set unique_ids = {101, 205, 330}; - + int search_key = 205; int missing_key = 400; // Check the count for the element 205 - std::cout << "Count for " << search_key << ": " + std::cout << "Count for " << search_key << ": " << unique_ids.count(search_key) << "\n"; - + // Check the count for the element 400 - std::cout << "Count for " << missing_key << ": " + std::cout << "Count for " << missing_key << ": " << unique_ids.count(missing_key) << "\n"; return 0; } -``` \ No newline at end of file +```