Summary: In this programming example, we will learn different methods to check if a key exists in a map in C++.

Method 1: Using map::count()

By using the built-in count method that is defined in the <map> header file, we can easily check whether a key exists in a C++ map or not.

The map::count(k) method searches the given map for elements with a key equivalent to k and returns the counts of the same.

Since a map in C++ maintains only the unique keys, the count method should return 1 in case the specified key exists in the map otherwise it will return 0.

#include <iostream>
#include <map>
using namespace std;

int main() {
    map<string, string> alphabets = {{ "A", "Apple"},
                                     { "B", "Bad Apple"},
                                     { "C", "Colored Apple"}};
    
    //count of the existing key should be 1
    if(alphabets.count("C") == 1){
        cout << "Key Exists!";
    } else {
        cout << "Key Doesn't Exists!";
    }
    
    return 0;
}

Method 2: Using map::find()

We can use another built-in method find to search for a specific key in the given map object.

The map::find(k) searches the map for an element with a key equivalent to k and returns an iterator to it if found otherwise it returns an iterator to map::end.

So, if the find method returns an iterator that is not equal to map::end, the map has an element with a specified key otherwise not.

#include <iostream>
#include <map>
using namespace std;

int main() {
    map<int, string> employees = {{ 7456, "Rohan Galileo"},
                                  { 1230, "Hitesh Rustom"},
                                  { 5809, "Jade Kumar"}};
    
    //check if key exists using map::find()
    if(employees.find(1230)){
        cout << "Key Exists!";
    } else {
        cout << "Key Doesn't Exists!";
    }
    
    return 0;
}

Method 3: Using map::contains()

In C++20, the map::contains(k) method was introduced whose sole purpose is to check whether an element with a key equivalent to k is present in the map or not.

The contains method returns true if the map has the key otherwise it returns false.

#include <iostream>
#include <map>
using namespace std;

int main() {
    map<int, int> cubes = {{2, 8},
                           {3, 27},
                           {4, 64}};
    
    //using map::contains() to check if key exists
    if(cubes.contains(5)){
        cout << "Key Exists!";
    } else {
        cout << "Key Doesn't Exists!";
    }
    
    return 0;
}

Method 4: Using for Loop

Alternatively, we can iterate through the given map using (traditional or range-based) for loop and try matching the search key with keys of the map elements to know if the key exists in the map or not.

#include <iostream>
#include <map>
using namespace std;

int main() {
    map<int, int> squares = {{2, 4},
                             {3, 9},
                             {4, 16}};
    
    //bool variable to store whether the key has been found    
    bool found = false;
    
    //iterate through the map elements                      
    for(auto x: squares){
        //check if any key is equal to 4
        if(x.first == 4){
            found = true;
            break;
        } 
    }
    
    //output the search result
    if(found){
        cout << "Key Exists!";
    } else {
        cout << "Key Doesn't Exists!";
    }
    
    return 0;
}

These were the 4 ways using which we can check if the given key exist in a map or not in C++.

Leave a Reply