Check if a Key Exists in a Map in C++

  1. Use the std::map::find Function to Check if Key Exists in a C++ Map
  2. Use the std::map::count Function to Check if Key Exists in a C++ Map
  3. Use the std::map::contains Function to Check if Key Exists in a C++ Map

This article will introduce how to check if a key exists in a map in C++.

Use the std::map::find Function to Check if Key Exists in a C++ Map

The std::map container is an associative data structure of key-value pairs stored sorted, and each element has a unique key. On the other hand, STL also provides an unsorted version of the same container named std::unordered_map. Both of these containers support the key searching methods that are described below.

find is one of the built-in functions of the std::map container that takes a single argument of corresponding key value to search for. The function returns an iterator to the element with the given key-value, otherwise, the past-the-end iterator. In the following example, we initialize the map of std::pair<string, string> types and then take the key value from the user input passed to the find function. The example program outputs the affirmative string to the cout stream.

#include <iostream>
#include <map>

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

int main(){
    string key_to_find;
    std::map<string, string> lang_map = {{"j", "Julia",},
                                         {"p", "Python",},
                                         {"m", "MATLAB",},
                                         {"o", "Octave",},
                                         {"s", "Scala",},
                                         {"l", "Lua",}};

    for (const auto & [key, value] : lang_map) {
        cout << key << " : " << value << endl;
    }

    cout << "Enter the key to search for: ";
    cin >> key_to_find;

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

    return EXIT_SUCCESS;
}

Output:

j : Julia
l : Lua
m : MATLAB
o : Octave
p : Python
s : Scala
Enter the key to search for: l
Key Exists!

Use the std::map::count Function to Check if Key Exists in a C++ Map

Alternatively, one can utilize the count built-in function of the std::map container to check if a given key exists in a map object. Note that, count function retrieves the number of elements that have the given key value. If no element is found with the key 0 value is returned. Thus, we can use the count function call as an if condition to output the affirming string when the given key exists in a map object.

#include <iostream>
#include <map>

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

int main(){
    string key_to_find;
    std::map<string, string> lang_map = {{"j", "Julia",},
                                         {"p", "Python",},
                                         {"m", "MATLAB",},
                                         {"o", "Octave",},
                                         {"s", "Scala",},
                                         {"l", "Lua",}};

    cout << "Enter the key to search for: ";
    cin >> key_to_find;

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

    return EXIT_SUCCESS;
}

Output:

Enter the key to search for: l
Key Exists!

Use the std::map::contains Function to Check if Key Exists in a C++ Map

contains is another built-in function that can be used to find if the key exists in a map. This function returns a boolean value if the element with the given key exists in the object. Notice that all three functions listed in this article have logarithmic complexity.

#include <iostream>
#include <map>

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

int main(){
    string key_to_find;
    std::map<string, string> lang_map = {{"j", "Julia",},
                                         {"p", "Python",},
                                         {"m", "MATLAB",},
                                         {"o", "Octave",},
                                         {"s", "Scala",},
                                         {"l", "Lua",}};

    cout << "Enter the key to search for: ";
    cin >> key_to_find;

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

    return EXIT_SUCCESS;
}

Output:

Enter the key to search for: l
Key Exists!
Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - C++ Map

  • Use an STL Map Container in C++
  • Find a Key in C++ Map