C++ std::map count() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::map

Definition

In C++, std::map::count() returns the number of pairs that have a given key. A std::map never holds the same key twice, so the answer is always 0 or 1.

That makes it a simple “does this key exist?” test. It does not read the value and it does not add the key. Include <map> to use it.

This is a member of std::map<Key, T>, a sorted container of key and value pairs. Each key is unique and the pairs are always kept in key order, so a map can find, insert, and erase a key in logarithmic time. Read a value with at() or find(). The m[key] operator also works, but it adds a default value when the key is missing.

Syntax

Pass the key you are looking for.

C++
if (m.count("Raj") == 1) {
    // key exists
}
bool has = m.count("Zoe") > 0;
CallWhat it does
m.count(key)Returns 1 if the key is in the map, and 0 if it is not.

count() only says whether the key exists. To also get the value, use find(). Since C++20, contains() expresses the same test more clearly. A std::multimap can return larger numbers, but a std::map cannot.

Library declarations
C++
size_type count(const Key& key) const;
template<class K> size_type count(const K& x) const;   // transparent comparator

Parameters

ParameterDescription
keyThe key to look for.

Return Value

Return ValueDescription
1The key is in the map.
0The key is not in the map.

C++ Examples

Example 1 — count() Returns 0 or 1

Ask for one key that exists and one that does not.

C++
#include <iostream>
#include <map>
#include <string>

int main() {
    std::map<std::string, int> age = {{"Mia", 21}, {"Raj", 25}};

    std::cout << "Raj: " << age.count("Raj") << '\n';
    std::cout << "Zoe: " << age.count("Zoe") << '\n';
    std::cout << "size: " << age.size() << '\n';
    return 0;
}

How It Works

  1. count("Raj") searches the keys and finds one match, so the result is 1.
  2. count("Zoe") finds none, so the result is 0. Because keys are unique, it can never be 2 or more.
  3. The size is still 2. Asking with count() does not add "Zoe", unlike age["Zoe"] would.

Example 2 — count() in a Condition

Add a word to a map only the first time you see it.

C++
#include <iostream>
#include <map>
#include <string>
#include <vector>

int main() {
    std::vector<std::string> input = {"red", "blue", "red", "green", "blue"};
    std::map<std::string, int> firstSeen;

    for (std::size_t i = 0; i < input.size(); ++i) {
        if (firstSeen.count(input[i]) == 0) {
            firstSeen[input[i]] = static_cast<int>(i);
        }
    }

    for (const auto& kv : firstSeen) {
        std::cout << kv.first << " first at index " << kv.second << '\n';
    }
    return 0;
}

How It Works

  1. The loop reads each word. count(word) == 0 is true only when the word has never been stored.
  2. For a new word, firstSeen[word] = i saves its index. The second red and the second blue find a count of 1, so they are skipped and the first index stays.
  3. The map prints in alphabetical order: blue, green, red, with indexes 1, 3, 0.

Example 3 — count() Added Up

Check how many of several wanted keys are present by adding the results.

C++
#include <iostream>
#include <map>
#include <string>
#include <vector>

int main() {
    std::map<std::string, double> menu = {{"tea", 2.5}, {"coffee", 3.0}, {"juice", 4.0}};
    std::vector<std::string> wanted = {"tea", "cake", "juice", "soup"};

    int available = 0;
    for (const std::string& item : wanted) {
        available += static_cast<int>(menu.count(item));
    }

    std::cout << available << " of " << wanted.size() << " items are on the menu\n";
    return 0;
}

How It Works

  1. menu has three drinks. wanted lists four items, two of which are not on the menu.
  2. Each menu.count(item) is 1 for tea and juice, and 0 for cake and soup. Adding them gives 2.
  3. This only works as a sum because the answer is always 0 or 1 for a std::map.

Frequently Asked Questions

The number of pairs with the given key. For a std::map that is only 0 or 1, because keys are unique.
No. It only looks. Using m[key] to test for a key is a mistake, because that inserts the key.
count() only says whether the key exists. find() returns an iterator, so you can also read or change the value without a second search.
They give the same answer for a map, but contains() (C++20) returns a bool and reads more clearly. count() works in every C++ version.
It is a short exists check, and the same function name works on containers where larger counts are possible, such as multimap.

Next: crbegin()

Keep going through the std::map functions. Get a read-only reverse iterator to the biggest key (C++11).

crbegin() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful