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

Are you looking for how to check if a given key exists in a map or not in c++? Then for this, we need to use the inbuilt find() function to check the given key.

Once you input the given key in the find function of ordered_map or unordered_map in C++, you will get True as return value if the key exists else you will reach the end of the unordered_map or ordered_map in C++.

Let me show you different ways how you can check if a given key exists in a map or not in C++.

Using find function of unordered_map

If you are using the unordered_map in c++ then it has an inbuilt function named as find(). Using find on the unordered_map object you can easily search and verify if the given key exists in the map or not.

As per documentation, the map::find function returns true if the key is found in the map and it returns the end of the map as false if the key does not exist.

Let us see it through an example code for the unordered_map thing. Printing the HashMap or Map to check if the function is correct or not if you have any doubts.

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

void isKeyPresent(char &x, std::unordered_map &newMapObj){
    if (newMapObj.find(ch) != newMapObj.end()) {
        std::cout << "Key found";
    }
    else {
        std::cout << "Key not found";
    }
}
 
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;
}

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

You can use STL algorithms to find if the given key is present in the unordered map or not.

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

//hepler function to check if key exist.
void isKeyPresent(char &x, std::unordered_map &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";
    }
    else {
        std::cout << "Key not found";
    }
}

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;
}

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 &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";
    }
    else {
        std::cout << "Key not found";
    }
}

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;
}

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 &newMapObj){
    bool keyFound = false;
    std::for_each(newMapObj.begin(), newMapObj.end(),
                [&x, &keyFound](std::pair<const char, int> &entry)
                    {
                        if (entry.first == x) {
                            found = true;
                        }
                });
    if (found) {
        std::cout << "Key found";
    }
    else {
        std::cout << "Key not found";
    }
}

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;
}

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 &newMapObj){
  
    bool keyFound = std::any_of(m.begin(), m.end(),
                    [&x](std::pair<const char, int> &entry) {
                        return (entry.first == ch);
                    });
    if (keyFound) {
        std::cout << "Key found";
    }
    else {
        std::cout << "Key not found";
    }
}

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;
}

Using count function of unordered_map

To check if a particular key is present in the map, you can use the count function which returns the values as true or false. Since there can be only one unique key in the map hence the count function if the key exists will return 1 otherwise if the 0 is returned 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 &newMapObj){
    if (newMapObj.count(ch)) {
        std::cout << "Key found";
    }
    else {
        std::cout << "Key not found";
    }
}

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;
}

Using map::contain function for C++ 20

If you are using C++ version 20, then it has introduced a new function for a map named map::contains that can also be used to check if the given key exists in the map or not.

Let us see in below example code for more understanding of the map::contains function. So, if the key exists in the map then contain function returns True otherwise it returns False if the key is not present in the map.

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

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;
}

If you liked our post related to the hash map implementation in JAVA 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.

Share your love

Leave a Reply