How To Check if a given key exists in a map or not in C++

Do you want to know how to determine whether or not a given key exists in a map? Then, to verify the given key, we must employ the built-in find() function.

If you enter the given key into the find function of an ordered map or unordered map in C++, you will get the return value True if the key exists; otherwise, you will reach the end of the unordered map or ordered map in C++.

Let me show you several C++ methods for determining whether or not a given key exists in a map.

Using the find function of unordered_map

When you use the unordered map in C++, you get a function called find (). By using find on the unordered map object, you can quickly search for and verify if the given key exists in the map.

The map::find function, according to the documentation, returns true if the key is found in the map and false if the key does not exist.

Let’s look at some example code for the unordered map.

#include <iostream>
#include <unordered_map>
#include <algorithm>

void isKeyPresent(char x, std::unordered_map<char, int> &newMapObj){
    if (newMapObj.find(x) != newMapObj.end()) {
        std::cout << "Key found"<<std::endl;
    }
    else {
        std::cout << "Key not found"<<std::endl;
    }
}
 
int main()
{
    std::unordered_map<char, int> newMapObj;
    
    newMapObj['a'] = 1;
    newMapObj['b'] = 2;
    newMapObj['c'] = 3;
    newMapObj['d'] = 4;
      
    //Since a is present in the newMapObj hence 
    //key found will be displayed.
    isKeyPresent('a', newMapObj);
  
    //Since x is not present as key in newMapObj hence 
    //key not found is displayed.
    isKeyPresent('x', newMapObj);

 
    return 0;
}

Output:

Key found
Key not found

Using std::find_if, std::for_each, std::any_of, and std::count_if functions

STL algorithms can be used to determine whether or not a given key exists in an unordered map.

#include <iostream>
#include <unordered_map>
#include <algorithm>

//hepler function to check if key exist.
void isKeyPresent(char x, std::unordered_map<char, int> &newMapObj){
  
    auto it = std::find_if(newMapObj.begin(), newMapObj.end(),
                [&x](std::pair<const char, int> &entry) {
                        return (entry.first == x);
                });
    if (it != newMapObj.end()) {
        std::cout << "Key found"<<std::endl;
    }
    else {
        std::cout << "Key not found"<<std::endl;
    }
}

int main()
{
    std::unordered_map<char, int> newMapObj;
    
    newMapObj['a'] = 1;
    newMapObj['b'] = 2;
    newMapObj['c'] = 3;
    newMapObj['d'] = 4;
      
    //Since a is present in the newMapObj hence key 
    //found will be displayed.
    isKeyPresent('a', newMapObj);
  
    //Since x is not present as key in newMapObj 
    //hence key not found is displayed.
    isKeyPresent('x', newMapObj);
 

 
    return 0;
}

Output:

Key found
Key not found

Below is the example code for checking for the key using the std::count_if function.

#include <iostream>
#include <unordered_map>
#include <algorithm>


//hepler function to check if key exist.
void isKeyPresent(char x, std::unordered_map<char, int> &newMapObj){
  
    int count = std::count_if(newMapObj.begin(), newMapObj.end(),
                [&x](std::pair<const char, int> &entry) {
                        return (entry.first == x);
                });
    if (count) {
        std::cout << "Key found"<<std::endl;
    }
    else {
        std::cout << "Key not found"<<std::endl;
    }
}

int main()
{
    std::unordered_map<char, int> newMapObj;
    
    newMapObj['a'] = 1;
    newMapObj['b'] = 2;
    newMapObj['c'] = 3;
    newMapObj['d'] = 4;
      
    //Since a is present in the newMapObj 
    //hence key found will be displayed.
    isKeyPresent('a', newMapObj);
  
    //Since x is not present as key in newMapObj 
    //hence key not found is displayed.
    isKeyPresent('x', newMapObj);
 

 
    return 0;
}

Output:

Key found
Key not found

Below is the example code for checking for the key using the std::for_each function.

#include <iostream>
#include <unordered_map>
#include <algorithm>
 
//hepler function to check if key exist.
void isKeyPresent(char x, std::unordered_map<char, int> &newMapObj){
    bool keyFound = false;
    std::for_each(newMapObj.begin(), newMapObj.end(),
                [&x, &keyFound](std::pair<const char, int> &entry)
                    {
                        if (entry.first == x) {
                            keyFound = true;
                        }
                });
    if (keyFound) {
        std::cout << "Key found"<<std::endl;
    }
    else {
        std::cout << "Key not found"<<std::endl;
    }
}

int main()
{
    std::unordered_map<char, int> newMapObj;
    
    newMapObj['a'] = 1;
    newMapObj['b'] = 2;
    newMapObj['c'] = 3;
    newMapObj['d'] = 4;
      
    //Since a is present in the newMapObj 
    //hence key found will be displayed.
    isKeyPresent('a', newMapObj);
  
    //Since x is not present as key in newMapObj
    //hence key not found is displayed.
    isKeyPresent('x', newMapObj);
 


    return 0;
}

Output:

Key found
Key not found

Below is the example code for checking for the key using the std::any_of function.

#include <iostream>
#include <unordered_map>
#include <algorithm>
 
//hepler function to check if key exist.
void isKeyPresent(char x, std::unordered_map<char, int> &newMapObj){
  
    bool keyFound = std::any_of(newMapObj.begin(), newMapObj.end(),
                    [&x](std::pair<const char, int> &entry) {
                        return (entry.first == x);
                    });
    if (keyFound) {
        std::cout << "Key found"<<std::endl;
    }
    else {
        std::cout << "Key not found"<<std::endl;
    }
}

int main()
{
    std::unordered_map<char, int> newMapObj;
    
    newMapObj['a'] = 1;
    newMapObj['b'] = 2;
    newMapObj['c'] = 3;
    newMapObj['d'] = 4;
      
    //Since a is present in the newMapObj 
    //hence key found will be displayed.
    isKeyPresent('a', newMapObj);
  
    //Since x is not present as key in newMapObj
    //hence key not found is displayed.
    isKeyPresent('x', newMapObj);
 

 
    return 0;
}

Output:

Key found
Key not found

Using the count function of unordered_map

To check whether a specific key exists in the map, use the count function, which returns true or false values. Given that there can be only one unique key in the map, the count function will return 1 if the key exists and 0 otherwise.

#include <iostream>
#include <unordered_map>
#include <algorithm>
 
//hepler function to check if key exist.
void isKeyPresent(char x, std::unordered_map<char, int> &newMapObj){
    if (newMapObj.count(x)) {
        std::cout << "Key found"<<std::endl;
    }
    else {
        std::cout << "Key not found"<<std::endl;
    }
}

int main()
{
    std::unordered_map<char, int> newMapObj;
    
    newMapObj['a'] = 1;
    newMapObj['b'] = 2;
    newMapObj['c'] = 3;
    newMapObj['d'] = 4;
      
    //Since a is present in the newMapObj 
    //hence key found will be displayed.
    isKeyPresent('a', newMapObj);
  
    //Since x is not present as key in newMapObj 
    //hence key not found is displayed.
    isKeyPresent('x', newMapObj);
 
    return 0;
}

Output:

Key found
Key not found

Using map::contain function for C++ 20

If you’re using C++ version 20, you’ll notice a new function for a map called map::contains, which can also be used to determine whether or not the given key exists in the map.

See the code below for a better understanding of the map::contains function. So, the map::contains function returns True if the key exists in the map; otherwise, it returns False if the key does not exist in the map.

#include <iostream>
#include <unordered_map>
#include <algorithm>
 
//hepler function to check if key exist.
void isKeyPresent(char x, std::unordered_map<char, int> &newMapObj){
    if (newMapObj.contains(x)) {
        std::cout << "Key found"<<std::endl;
    }
    else {
        std::cout << "Key not found"<<std::endl;
    }
}

int main()
{
    std::unordered_map<char, int> newMapObj;
    
    newMapObj['a'] = 1;
    newMapObj['b'] = 2;
    newMapObj['c'] = 3;
    newMapObj['d'] = 4;
      
    //Since a is present in the newMapObj 
    //hence key found will be displayed.
    isKeyPresent('a', newMapObj);
  
    //Since x is not present as key in newMapObj 
    //hence key not found is displayed.
    isKeyPresent('x', newMapObj);
 
    return 0;
}

Output:

Key found
Key not found

Wrap Up

If you liked our post related to how to check if a given key exists in a map in C++ then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Wanna read more interview-related questions? Check Other Posts Related to Data Structures.

Leave a Comment