Find a Key in C++ Map

  1. Use std::map::find Method to Find a Key in a Map
  2. Use std::map::count Method to Find a Key in a Map
  3. Use std::map::contains Method to Find a Key in a Map

This article explains how to find out if a specified key exists in a given map in C++. Note that all discussed methods have logarithmic time complexity.

Use std::map::find Method to Find a Key in a Map

To look up the specific key, we can use the find method, which traverses the map and returns the corresponding element. If it finds no element with a given key, the return value is the map.end() iterator. So, we exploit this feature and compare two iterator values in the if statement and print output:

#include <iostream>
#include <map>

using std::cout;
using std::cin;
using std::endl;
using std::map;
using std::string;

int main(){
    map<string, string> veggy_map = {{"a", "Asparagus",},
                                   {"b", "Beetroot",},
                                   {"g", "Ginger",},
                                   {"m", "Melon",},
                                   {"p", "Pumpkin",},
                                   {"s", "Spinach",}};
    string key_to_find = "m";

    if (veggy_map.find(key_to_find) != veggy_map.end()) {
        cout << "Key Exists!" << endl;
    }

    return EXIT_SUCCESS;
}

Output:

Key Exists!

Use std::map::count Method to Find a Key in a Map

Another method to search for a specific key is count. This function counts the number of elements associated with a given key. Since the std::map container does not allow duplicate keys, we can consider the answer to our problem affirmative if this method’s return value does not equal to zero.

#include <iostream>
#include <map>

using std::cout;
using std::cin;
using std::endl;
using std::map;
using std::string;

int main(){
    map<string, string> veggy_map = {{"a", "Asparagus",},
                                   {"b", "Beetroot",},
                                   {"g", "Ginger",},
                                   {"m", "Melon",},
                                   {"p", "Pumpkin",},
                                   {"s", "Spinach",}};
    string key_to_find = "m";

    if (veggy_map.count(key_to_find)) {
        cout << "Key Exists!" << endl;
    }

    return EXIT_SUCCESS;
}

If the given container is multimap, you can reuse the return value from the count method and pass it as several loop iterations as shown in the following example.

#include <iostream>
#include <map>

using std::cout;
using std::cin;
using std::endl;
using std::multimap;
using std::string;

int main(){
    multimap<string, string> veggy_map = {{"a", "Asparagus",},
                                   {"b", "Beetroot",},
                                   {"b", "Broccoli",},
                                   {"g", "Ginger",},
                                   {"m", "Melon",},
                                   {"p", "Pumpkin",},
                                   {"s", "Spinach",}};
    string key_to_find = "b";

    auto ret = veggy_map.count(key_to_find);
    while (ret) {
        cout << "Key Exists!" << endl;
        ret -= 1;
    }

    return EXIT_SUCCESS;
}

Output:

Key Exists!
Key Exists!

Use std::map::contains Method to Find a Key in a Map

This version utilizes the C++20 method contains, which is best suited for this task. The method returns true boolean value if the specified key element is found in the map.

#include <iostream>
#include <map>

using std::cout;
using std::cin;
using std::endl;
using std::map;
using std::string;

int main(){
    map<string, string> veggy_map = {{"a", "Asparagus",},
                                   {"b", "Beetroot",},
                                   {"g", "Ginger",},
                                   {"m", "Melon",},
                                   {"p", "Pumpkin",},
                                   {"s", "Spinach",}};
    string key_to_find = "m";

    if (veggy_map.contains(key_to_find)) {
        cout << "Key Exists!" << endl;
    }

    return EXIT_SUCCESS;
}
Write for us
DelftStack articles are written by software geeks like you. If you also would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - C++ Map

  • Insert New Elements in an STL Map in C++
  • Use the std::map::find Function in C++
  • Use an STL Map Container in C++
  • Use the STL Unordered Map Container in C++