From f008f089c6500e1ee826e98b23b425bba2cddd08 Mon Sep 17 00:00:00 2001 From: ebikatsudon Date: Sat, 19 Oct 2024 00:18:08 -0700 Subject: [PATCH 01/13] added count function entry --- .../cpp/concepts/maps/terms/count/count.md | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 content/cpp/concepts/maps/terms/count/count.md diff --git a/content/cpp/concepts/maps/terms/count/count.md b/content/cpp/concepts/maps/terms/count/count.md new file mode 100644 index 00000000000..78b94e95584 --- /dev/null +++ b/content/cpp/concepts/maps/terms/count/count.md @@ -0,0 +1,90 @@ +--- +Title: 'count()' +Description: 'Checks if a given key occurs in the map container.' +Subjects: + - 'Computer Science' + - 'Game Development' + - 'Machine Learning' +Tags: + - 'Objects' + - 'OOP' + - 'Classes' + - 'Map' +CatalogContent: + - 'learn-c-plus-plus-functions' + - 'learn-c-plus-plus' + - 'paths/computer-science' + - 'paths/c' +--- + +The **`count()`** method checks if a map contains any elements with a specified `key`. As all keys in a map are unique, the function will return 1 if the element exists in the container and 0 if it does not. + +## Syntax + +```pseudo +mapName.count(key) +``` + +`key` is the value that will be searched for in `mapName`. + +## Codebyte Examples + +In the following example, the `count()` function is used to check whether the keys `"coconuts"` and `"strawberries"` exist in the `fruits` map. If the key exists, it will print the corresponding integer value. + +```codebyte/cpp +#include +#include + +int main() { + // Initializing map with items + std::map fruits {{"apples", 50}, {"bananas", 100}, {"coconuts", 20}, {"dates", 500}}; + + // Checking if "coconuts" exists + std::string key = "coconuts"; + + if ( fruits.count("coconuts") > 0) { + std::cout << " There are " << fruits[key] << " " << key << ".\n"; + } else { + std::cout << " There are no " << key << ".\n"; + } + + // Checking if "strawberries" exists + + key = "strawberries"; + + if ( fruits.count("strawberries") > 0) { + std::cout << " There are " << fruits[key] << " " << key << ".\n"; + } else { + std::cout << " There are no " << key << ".\n"; + } + + return 0; +} +``` + +The example below illustrates a scenario where the count function is used to check if an array of elements exists in a map. + +```codebyte/cpp +#include +#include + +int main() { + // Initializing map with items + std::map zoo_animals {{"hippos", 2}, {"lions", 4}, {"zebras", 6}, {"gorillas", 8}}; + std::string animals; + + // Creating array of animals + std::string animals_to_check[] = {"bats", "giraffes", "gorillas", "hippos", "zebras"}; + + // Loop through the animals and check if each one exists in the map + for (const auto& animals : animals_to_check) { + if (zoo_animals.count(animals) > 0) { + std::cout << " The zoo has " << zoo_animals[animals] << " " << animals << ".\n"; + } else { + std::cout << " The zoo does not have " << animals << ".\n"; + } + } + + return 0; +} +``` From dd23ca19f548c77050539f55374be77a212db142 Mon Sep 17 00:00:00 2001 From: ebikatsudon Date: Mon, 21 Oct 2024 13:43:34 -0700 Subject: [PATCH 02/13] minor edits --- .../cpp/concepts/maps/terms/count/count.md | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/content/cpp/concepts/maps/terms/count/count.md b/content/cpp/concepts/maps/terms/count/count.md index 78b94e95584..658f9a4d99a 100644 --- a/content/cpp/concepts/maps/terms/count/count.md +++ b/content/cpp/concepts/maps/terms/count/count.md @@ -29,11 +29,12 @@ mapName.count(key) ## Codebyte Examples -In the following example, the `count()` function is used to check whether the keys `"coconuts"` and `"strawberries"` exist in the `fruits` map. If the key exists, it will print the corresponding integer value. +In the following example, the `count()` function is used to check whether the keys `"coconuts"` and `"strawberries"` exist in the `fruits` map. ```codebyte/cpp #include #include +#include int main() { // Initializing map with items @@ -42,20 +43,20 @@ int main() { // Checking if "coconuts" exists std::string key = "coconuts"; - if ( fruits.count("coconuts") > 0) { - std::cout << " There are " << fruits[key] << " " << key << ".\n"; + if (fruits.count(key) > 0) { + std::cout << "There are " << fruits[key] << " " << key << ".\n"; } else { - std::cout << " There are no " << key << ".\n"; + std::cout << "There are no " << key << ".\n"; } // Checking if "strawberries" exists key = "strawberries"; - if ( fruits.count("strawberries") > 0) { - std::cout << " There are " << fruits[key] << " " << key << ".\n"; + if (fruits.count(key) > 0) { + std::cout << "There are " << fruits[key] << " " << key << ".\n"; } else { - std::cout << " There are no " << key << ".\n"; + std::cout << "There are no " << key << ".\n"; } return 0; @@ -67,11 +68,11 @@ The example below illustrates a scenario where the count function is used to che ```codebyte/cpp #include #include +#include int main() { // Initializing map with items std::map zoo_animals {{"hippos", 2}, {"lions", 4}, {"zebras", 6}, {"gorillas", 8}}; - std::string animals; // Creating array of animals std::string animals_to_check[] = {"bats", "giraffes", "gorillas", "hippos", "zebras"}; @@ -79,9 +80,9 @@ int main() { // Loop through the animals and check if each one exists in the map for (const auto& animals : animals_to_check) { if (zoo_animals.count(animals) > 0) { - std::cout << " The zoo has " << zoo_animals[animals] << " " << animals << ".\n"; + std::cout << "The zoo has " << zoo_animals[animals] << " " << animals << ".\n"; } else { - std::cout << " The zoo does not have " << animals << ".\n"; + std::cout << "The zoo does not have " << animals << ".\n"; } } From c82464de0c8f4fc4fc5d13a9999b4874eeb1611a Mon Sep 17 00:00:00 2001 From: ebikatsudon <99709771+ebikatsudon@users.noreply.github.com> Date: Tue, 22 Oct 2024 12:29:47 -0700 Subject: [PATCH 03/13] Update content/cpp/concepts/maps/terms/count/count.md Co-authored-by: Mamta Wardhani --- content/cpp/concepts/maps/terms/count/count.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/content/cpp/concepts/maps/terms/count/count.md b/content/cpp/concepts/maps/terms/count/count.md index 658f9a4d99a..0fb663d0316 100644 --- a/content/cpp/concepts/maps/terms/count/count.md +++ b/content/cpp/concepts/maps/terms/count/count.md @@ -1,6 +1,6 @@ --- -Title: 'count()' -Description: 'Checks if a given key occurs in the map container.' +Title: '.count()' +Description: 'Checks whether a specified key exists in the map and returns the number of occurrences' Subjects: - 'Computer Science' - 'Game Development' @@ -11,10 +11,8 @@ Tags: - 'Classes' - 'Map' CatalogContent: - - 'learn-c-plus-plus-functions' - 'learn-c-plus-plus' - 'paths/computer-science' - - 'paths/c' --- The **`count()`** method checks if a map contains any elements with a specified `key`. As all keys in a map are unique, the function will return 1 if the element exists in the container and 0 if it does not. From a4370e5017e2f454ae4bc4f83f7e9319fb671fc6 Mon Sep 17 00:00:00 2001 From: ebikatsudon <99709771+ebikatsudon@users.noreply.github.com> Date: Tue, 22 Oct 2024 12:33:32 -0700 Subject: [PATCH 04/13] Update content/cpp/concepts/maps/terms/count/count.md Co-authored-by: Mamta Wardhani --- content/cpp/concepts/maps/terms/count/count.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/cpp/concepts/maps/terms/count/count.md b/content/cpp/concepts/maps/terms/count/count.md index 0fb663d0316..2a53eafa9c4 100644 --- a/content/cpp/concepts/maps/terms/count/count.md +++ b/content/cpp/concepts/maps/terms/count/count.md @@ -15,7 +15,7 @@ CatalogContent: - 'paths/computer-science' --- -The **`count()`** method checks if a map contains any elements with a specified `key`. As all keys in a map are unique, the function will return 1 if the element exists in the container and 0 if it does not. +The **`.count()`** method checks if a map contains an element with a specified key. Since all keys in a map are unique, the function returns 1 if the key exists in the container and 0 if it does not. ## Syntax From 9a979d2ce210bb039d817a5b4ac7afd0ab8cc86e Mon Sep 17 00:00:00 2001 From: ebikatsudon <99709771+ebikatsudon@users.noreply.github.com> Date: Tue, 22 Oct 2024 12:33:55 -0700 Subject: [PATCH 05/13] Update content/cpp/concepts/maps/terms/count/count.md Co-authored-by: Mamta Wardhani --- content/cpp/concepts/maps/terms/count/count.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/content/cpp/concepts/maps/terms/count/count.md b/content/cpp/concepts/maps/terms/count/count.md index 2a53eafa9c4..b597a3585b7 100644 --- a/content/cpp/concepts/maps/terms/count/count.md +++ b/content/cpp/concepts/maps/terms/count/count.md @@ -23,7 +23,8 @@ The **`.count()`** method checks if a map contains an element with a specified k mapName.count(key) ``` -`key` is the value that will be searched for in `mapName`. +- `mapName`: Refers to the specific map being accessed. +- `key`: Represents the value that will be searched for in `mapName`. ## Codebyte Examples From 5f0f6a96a0433b32de5e181456900b97863e87ec Mon Sep 17 00:00:00 2001 From: ebikatsudon <99709771+ebikatsudon@users.noreply.github.com> Date: Tue, 22 Oct 2024 12:48:44 -0700 Subject: [PATCH 06/13] Update content/cpp/concepts/maps/terms/count/count.md Co-authored-by: Mamta Wardhani --- content/cpp/concepts/maps/terms/count/count.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/cpp/concepts/maps/terms/count/count.md b/content/cpp/concepts/maps/terms/count/count.md index b597a3585b7..c2b0c32c840 100644 --- a/content/cpp/concepts/maps/terms/count/count.md +++ b/content/cpp/concepts/maps/terms/count/count.md @@ -26,7 +26,7 @@ mapName.count(key) - `mapName`: Refers to the specific map being accessed. - `key`: Represents the value that will be searched for in `mapName`. -## Codebyte Examples +## Example In the following example, the `count()` function is used to check whether the keys `"coconuts"` and `"strawberries"` exist in the `fruits` map. From 9a567d4f54e6912dd84ccc7a6ea56009d0e006ea Mon Sep 17 00:00:00 2001 From: ebikatsudon <99709771+ebikatsudon@users.noreply.github.com> Date: Tue, 22 Oct 2024 12:49:20 -0700 Subject: [PATCH 07/13] Update content/cpp/concepts/maps/terms/count/count.md Co-authored-by: Mamta Wardhani --- content/cpp/concepts/maps/terms/count/count.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/cpp/concepts/maps/terms/count/count.md b/content/cpp/concepts/maps/terms/count/count.md index c2b0c32c840..59376449d98 100644 --- a/content/cpp/concepts/maps/terms/count/count.md +++ b/content/cpp/concepts/maps/terms/count/count.md @@ -28,7 +28,7 @@ mapName.count(key) ## Example -In the following example, the `count()` function is used to check whether the keys `"coconuts"` and `"strawberries"` exist in the `fruits` map. +In the following example, the `.count()` method is used to check whether the keys `"coconuts"` and `"strawberries"` exist in the `fruits` map: ```codebyte/cpp #include From c1ddcebd9e8a12ad69c142555875c32aa5681cd4 Mon Sep 17 00:00:00 2001 From: ebikatsudon <99709771+ebikatsudon@users.noreply.github.com> Date: Tue, 22 Oct 2024 12:49:39 -0700 Subject: [PATCH 08/13] Update content/cpp/concepts/maps/terms/count/count.md Co-authored-by: Mamta Wardhani --- content/cpp/concepts/maps/terms/count/count.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/cpp/concepts/maps/terms/count/count.md b/content/cpp/concepts/maps/terms/count/count.md index 59376449d98..54705a06813 100644 --- a/content/cpp/concepts/maps/terms/count/count.md +++ b/content/cpp/concepts/maps/terms/count/count.md @@ -30,7 +30,7 @@ mapName.count(key) In the following example, the `.count()` method is used to check whether the keys `"coconuts"` and `"strawberries"` exist in the `fruits` map: -```codebyte/cpp +```cpp #include #include #include From e4d0d4d34953c144a3f79e929702b4336a586df9 Mon Sep 17 00:00:00 2001 From: ebikatsudon <99709771+ebikatsudon@users.noreply.github.com> Date: Tue, 22 Oct 2024 12:57:50 -0700 Subject: [PATCH 09/13] Update content/cpp/concepts/maps/terms/count/count.md Co-authored-by: Mamta Wardhani --- content/cpp/concepts/maps/terms/count/count.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/content/cpp/concepts/maps/terms/count/count.md b/content/cpp/concepts/maps/terms/count/count.md index 54705a06813..85de2a050f7 100644 --- a/content/cpp/concepts/maps/terms/count/count.md +++ b/content/cpp/concepts/maps/terms/count/count.md @@ -62,7 +62,10 @@ int main() { } ``` -The example below illustrates a scenario where the count function is used to check if an array of elements exists in a map. + +## Codebyte Example + +The example below illustrates a scenario in which the count function is used to check whether an array of elements exists in a map: ```codebyte/cpp #include From 1a8381cfa9fd4faf081aba5bc6329c9d94296ce2 Mon Sep 17 00:00:00 2001 From: ebikatsudon Date: Tue, 22 Oct 2024 15:08:57 -0700 Subject: [PATCH 10/13] added example output --- content/cpp/concepts/maps/terms/count/count.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/content/cpp/concepts/maps/terms/count/count.md b/content/cpp/concepts/maps/terms/count/count.md index 85de2a050f7..343a9d092fc 100644 --- a/content/cpp/concepts/maps/terms/count/count.md +++ b/content/cpp/concepts/maps/terms/count/count.md @@ -62,6 +62,12 @@ int main() { } ``` +The result will look like this: + +```shell +There are 20 coconuts. +There are no strawberries. +``` ## Codebyte Example From b806381cbd46789416f7ffd47c970b88c89278af Mon Sep 17 00:00:00 2001 From: ebikatsudon Date: Tue, 22 Oct 2024 15:40:46 -0700 Subject: [PATCH 11/13] minor wording change --- content/cpp/concepts/maps/terms/count/count.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/cpp/concepts/maps/terms/count/count.md b/content/cpp/concepts/maps/terms/count/count.md index 343a9d092fc..021c7af2799 100644 --- a/content/cpp/concepts/maps/terms/count/count.md +++ b/content/cpp/concepts/maps/terms/count/count.md @@ -62,7 +62,7 @@ int main() { } ``` -The result will look like this: +The above code produces the following output: ```shell There are 20 coconuts. From 39a2a8182508aff55a114666221ebf063b879b07 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Wed, 23 Oct 2024 11:37:01 +0530 Subject: [PATCH 12/13] Update count.md minor fixes --- .../cpp/concepts/maps/terms/count/count.md | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/content/cpp/concepts/maps/terms/count/count.md b/content/cpp/concepts/maps/terms/count/count.md index 021c7af2799..34e726f52f3 100644 --- a/content/cpp/concepts/maps/terms/count/count.md +++ b/content/cpp/concepts/maps/terms/count/count.md @@ -15,8 +15,10 @@ CatalogContent: - 'paths/computer-science' --- -The **`.count()`** method checks if a map contains an element with a specified key. Since all keys in a map are unique, the function returns 1 if the key exists in the container and 0 if it does not. - +The **`.count()`** method in C++ for `std::map` is used to determine the presence of a specific key in the map. Since all keys in a map are unique, the function returns: +- *1* if the key exists in the container. +- *0* if the key does not exist. + ## Syntax ```pseudo @@ -37,25 +39,24 @@ In the following example, the `.count()` method is used to check whether the key int main() { // Initializing map with items - std::map fruits {{"apples", 50}, {"bananas", 100}, {"coconuts", 20}, {"dates", 500}}; + std::map fruits {{"apples", 50}, {"bananas", 100}, {"coconuts", 20}, {"dates", 500}}; // Checking if "coconuts" exists std::string key = "coconuts"; if (fruits.count(key) > 0) { - std::cout << "There are " << fruits[key] << " " << key << ".\n"; + std::cout << "There are " << fruits[key] << " " << key << ".\n"; // If key exists, print the count } else { - std::cout << "There are no " << key << ".\n"; + std::cout << "There are no " << key << ".\n"; // If key does not exist, print a message } // Checking if "strawberries" exists - key = "strawberries"; if (fruits.count(key) > 0) { - std::cout << "There are " << fruits[key] << " " << key << ".\n"; + std::cout << "There are " << fruits[key] << " " << key << ".\n"; // If key exists, print the count } else { - std::cout << "There are no " << key << ".\n"; + std::cout << "There are no " << key << ".\n"; // If key does not exist, print a message } return 0; @@ -71,7 +72,7 @@ There are no strawberries. ## Codebyte Example -The example below illustrates a scenario in which the count function is used to check whether an array of elements exists in a map: +The example below illustrates a scenario in which the `.count()` method is used to check whether an array of elements exists in a map: ```codebyte/cpp #include @@ -79,13 +80,10 @@ The example below illustrates a scenario in which the count function is used to #include int main() { - // Initializing map with items std::map zoo_animals {{"hippos", 2}, {"lions", 4}, {"zebras", 6}, {"gorillas", 8}}; - // Creating array of animals std::string animals_to_check[] = {"bats", "giraffes", "gorillas", "hippos", "zebras"}; - // Loop through the animals and check if each one exists in the map for (const auto& animals : animals_to_check) { if (zoo_animals.count(animals) > 0) { std::cout << "The zoo has " << zoo_animals[animals] << " " << animals << ".\n"; From f20bbca3ce54dd7cdda8e75d32e883ac6c53400c Mon Sep 17 00:00:00 2001 From: Daksha Deep Date: Fri, 8 Nov 2024 10:36:19 +0530 Subject: [PATCH 13/13] Formating done --- content/cpp/concepts/maps/terms/count/count.md | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/content/cpp/concepts/maps/terms/count/count.md b/content/cpp/concepts/maps/terms/count/count.md index 34e726f52f3..394d2e1d3bc 100644 --- a/content/cpp/concepts/maps/terms/count/count.md +++ b/content/cpp/concepts/maps/terms/count/count.md @@ -16,9 +16,10 @@ CatalogContent: --- The **`.count()`** method in C++ for `std::map` is used to determine the presence of a specific key in the map. Since all keys in a map are unique, the function returns: -- *1* if the key exists in the container. -- *0* if the key does not exist. - + +- _1_ if the key exists in the container. +- _0_ if the key does not exist. + ## Syntax ```pseudo @@ -81,9 +82,9 @@ The example below illustrates a scenario in which the `.count()` method is used int main() { std::map zoo_animals {{"hippos", 2}, {"lions", 4}, {"zebras", 6}, {"gorillas", 8}}; - + std::string animals_to_check[] = {"bats", "giraffes", "gorillas", "hippos", "zebras"}; - + for (const auto& animals : animals_to_check) { if (zoo_animals.count(animals) > 0) { std::cout << "The zoo has " << zoo_animals[animals] << " " << animals << ".\n";