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

Do you want to know how to check if a given key exists in a map or not? Then, to check the given key, we must use the inbuilt find() function.

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

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

Using find function of unordered_map

If you use the unordered map in C++, it comes with a function called find (). You can easily search and verify if the given key exists in the map by using find on the unordered map object.

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

Let’s take a look at an example code for the unordered map thing.

#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 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. Because there can only be one unique key in the map, the count function will return 1 if the key exists, otherwise 0 if the key does not exist.

#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.

For a better understanding of the map::contains function, see the example code below. So, if the key exists in the map, the contain function returns True; otherwise, if the key does not exist in the map, the contain function returns False.

#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 Post Related to Data Structures.

Leave a Comment