Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C++ Count function map #5543

Merged
merged 17 commits into from
Nov 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions content/cpp/concepts/maps/terms/count/count.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
---
Title: '.count()'
Description: 'Checks whether a specified key exists in the map and returns the number of occurrences'
Subjects:
- 'Computer Science'
- 'Game Development'
- 'Machine Learning'
Tags:
- 'Objects'
- 'OOP'
- 'Classes'
- 'Map'
CatalogContent:
- 'learn-c-plus-plus'
- 'paths/computer-science'
---

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
mapName.count(key)
```

- `mapName`: Refers to the specific map being accessed.
- `key`: Represents the value that will be searched for in `mapName`.

## Example

In the following example, the `.count()` method is used to check whether the keys `"coconuts"` and `"strawberries"` exist in the `fruits` map:

```cpp
#include <iostream>
#include <map>
#include <string>

int main() {
// Initializing map with items
std::map<std::string, int> 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"; // If key exists, print the count
} else {
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"; // If key exists, print the count
} else {
std::cout << "There are no " << key << ".\n"; // If key does not exist, print a message
}

return 0;
}
```

ebikatsudon marked this conversation as resolved.
Show resolved Hide resolved
The above code produces the following output:

```shell
There are 20 coconuts.
There are no strawberries.
```

## Codebyte Example

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 <iostream>
#include <map>
#include <string>

int main() {
std::map<std::string, int> 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";
} else {
std::cout << "The zoo does not have " << animals << ".\n";
}
}

return 0;
}
```
Loading