Check if key exists in map c++: In the previous article, we have discussed about How to use vector efficiently in C++ ?. Let us learn how to Check if a Given Key Exists in a Map in C++ Program.
Check if a Given Key Exists in Map
How to check if a key is in a map c++: In this article, we will see different ways to check whether a given key exists in a map or not.
As we know Map stores values in Key-Value pair. And we can use two member function to check if a key exists or not.
Let’s see one by one.
Method-1 : By using std::map::count
C++ map check if key exists: A member function count()
is provided by std::map.
It provides the count of number of elements in map with that particular key. As we know map contains elements with unique key only. So, 1 will be returned if key exists or else 0 will be returned.
Let’s see the below program to understand it clearly.
#include <iostream> #include <map> #include <string> int main() { //sample_Map as a Map with keys and values std::map<std::string, int> sample_Map = { { "A", 1 }, { "B", 2 }, { "C", 2 }, { "D", 3 } }; // Checking if key 'A' exists in the map if (sample_Map.count("A") > 0) { std::cout << "'A' Found" << std::endl; } else { std::cout << "'A' Not Found" << std::endl; } // Checking if key 'B' exists in the map if (sample_Map.count("B") > 0) { std::cout << "'B' Found" << std::endl; } else { std::cout << "'B' Not Found" << std::endl; } // Checking if key 'E' exists in the map if (sample_Map.count("E") > 0) { std::cout << "'E' Found" << std::endl; } else { std::cout << "'E' Not Found" << std::endl; } return 0; }
OutputĀ : 'A' Found 'B' Found 'E' Not Found
From the above example by using std::map::count
, we only saw that we only got to know that the key exists or not. But if we want to find the key along with associated value then we can use std::map::find
.
Method-2 : By using std::map::find
C++ map key exists: A member function find()
is provided by std::map.
Which helps in accessing a particular key along with it’s associated value. Means if any element with that given key is found in the map then it returns its iterator so that we access key and associated value or else it returns the end of map.
Let’s see the below program to understand it clearly.
#include <iostream> #include <map> #include <string> #include <iterator> #include <algorithm> int main() { // sample_map as Map std::map<std::string, int> sample_map = { { "A", 1 }, { "B", 2 }, { "C", 2 }, { "D", 3 } }; // An iterator of map created std::map<std::string, int>::iterator it; // Finding the element with key 'A' it = sample_map.find("A"); // Checking if element exists in map or not if (it != sample_map.end()) { // Element with key 'A' found std::cout << "'A' Found" << std::endl; // Accessing the Key from iterator std::string key = it->first; // Accessing the Value from iterator int value = it->second; std::cout << "key = " << key << " :: Value = " << value << std::endl; } else { // Element with key 'A' Not Found std::cout << "'A' Not Found" << std::endl; } // Finding the element with key 'B' it = sample_map.find("B"); // Checking if element exists in map or not if (it != sample_map.end()) { // Element with key 'B' found std::cout << "'B' Found" << std::endl; // Accessing the Key from iterator std::string key = it->first; // Accessing the Value from iterator int value = it->second; std::cout << "key = " << key << " :: Value = " << value << std::endl; } else { // Element with key 'B' Not Found std::cout << "'A' Not Found" << std::endl; } // Finding the element with key 'E' it = sample_map.find("E"); // Checking if element exists in map or not if (it != sample_map.end()) { // Element with key 'E' found std::cout << "'E' Found" << std::endl; // Accessing the Key from iterator std::string key = it->first; // Accessing the Value from iterator int value = it->second; std::cout << "key = " << key << " :: Value = " << key << std::endl; } else { // Element with key 'E' Not Found std::cout << "'E' Not Found" << std::endl; } return 0; }
Output : 'A' Found key = A :: Value = 1 'B' Found key = B :: Value = 2 'E' Not Found