In this article, we will discuss different ways to check if a given key exists in map or not.
Table of Contents
Map internally store elements in Key-Value pair. It provides 3 member functions to check if a given key exists in map . We will discuss them one by one.
Check if map contains a key using Contains() Function
C++20 introduced a new member function for std::map i.e. contains() function. It accepts a key as an argument and returns true, if the given key exists in the Map. If the map does not contains the given key, then it will return false. Let’s see complete example,
#include <iostream> #include <map> #include <string> int main() { std::map<std::string, int> wordFrequency { {"this", 22}, {"why", 33}, {"what", 67}, {"how", 41} }; if (wordFrequency.contains("this")) { // The key exists std::cout << "Key exists in the map" << std::endl; } else { // The key does not exist std::cout << "Key does not exist in map" << std::endl; } return 0; }
Output:
Key exists in the map
To use map::contains() function, you need a C++20 compiler. If you are working with older versions of C++, then checkout the next two ways.
Check if map contains a key using Count() Function
std::map provides a member function count() i.e.
size_type count (const key_type& K) const;
It finds and returns the count of number of elements in map with key K. As map contains elements with unique key only. So, it will return 1 if key exists else 0.
Suppose we have map of string & int i.e.
std::map<std::string, int> wordFrequency { {"this", 22}, {"why", 33}, {"what", 67}, {"how", 41} };
Let’s check if key ‘this’ exists in the map or not i.e.
if (wordFrequency.count("this") > 0) { // The key exists std::cout << "Key exists in the map" << std::endl; }
Complete example as follows,
#include <iostream> #include <map> #include <string> int main() { std::map<std::string, int> wordFrequency { {"this", 22}, {"why", 33}, {"what", 67}, {"how", 41} }; if (wordFrequency.count("this") > 0) { // The key exists std::cout << "Key exists in the map" << std::endl; } else { // The key does not exist std::cout << "Key does not exist in map" << std::endl; } return 0; }
Output:
Key exists in the map
std::map::count() function just tells if the given key exists in map or not. But what if we also want to access the value associated with the given key. For that map provides another member function i.e. std::map::find().
Check if map contains a key using Find() Function
std::map provides a member function find() i.e.
iterator find (const key_type& k);
It checks if any element with given key ‘this’ exists in the map and if yes then it returns its iterator else
it returns the end of map.
Suppose we have map of string & int i.e.
std::map<std::string, int> wordFrequency { {"this", 22}, {"why", 33}, {"what", 67}, {"how", 41} };
Let’s check if key ‘this’ exists in the map or not i.e.
// Create an iterator of map std::map<std::string, int>::iterator it; // Find the element with key 'this' it = wordFrequency.find("this"); // Check if element exists in map or not if (it != wordFrequency.end()) { // Element with key 'this' found std::cout << "'this' Found" << std::endl; // Access the Key from iterator std::string key = it->first; // Access the Value from iterator int value = it->second; std::cout << "key = " << key << " :: Value = " << key << std::endl; }
Output:
'this' Found key = this :: Value = this
Summary
Today, we learned about different ways to check if a map contains a key or not in C++.
Pingback: std::map Tutorial Part 1: Usage Detail with examples – thisPointer.com