We are committed to provide service 24 X 7

Deals, Shopping, Training, Tools

How check if a given key exists in a Map | C++

In this article we will discuss how to find if a given key exists in map or not.


Map internally store elements in Key-Value pair. It provides 2 member functions to check if a given key exists in map i.e.

Advertisements

  • std::map::find
  • std::map::count

Check if map contains a key using std::map::count

std::map provides a member function count() i.e.

size_type count (const key_type& K) const;

It finds & 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> wordMap = {
				{ "is", 6 },
				{ "the", 5 },
				{ "hat", 9 },
				{ "at", 6 }
			};

Let’s check if key ‘hat’ exists in the map or not i.e.

if (wordMap.count("hat") > 0)
{
	std::cout << "'hat' Found" << std::endl;
}

Complete example as follows,

#include <iostream>
#include <map>
#include <string>

int main() {

	// Map of string & int i.e. words as key & there
	// occurrence count as values
	std::map<std::string, int> wordMap = {
						{ "is", 6 },
						{ "the", 5 },
						{ "hat", 9 },
						{ "at", 6 }
					};


	// Check if key 'hat' exists in the map
	if (wordMap.count("hat") > 0)
	{
		std::cout << "'hat' Found" << std::endl;
	}
	else
	{
		std::cout << "'hat' Not Found" << std::endl;
	}

	// Check if key 'hello' exists in the map
	if (wordMap.count("hello") > 0)
	{
		std::cout << "'hello' Found" << std::endl;
	}
	else
	{
		std::cout << "'hello' Not Found" << std::endl;
	}

	return 0;
}

Output:

'hat' Found
'hello' Not Found

std::map::count 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 std::map::find

std::map provides a member function find() i.e.

iterator find (const key_type& k);

It checks if any element with given key ‘k’ 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> wordMap = {
				{ "is", 6 },
				{ "the", 5 },
				{ "hat", 9 },
				{ "at", 6 }
			};

Let’s check if key ‘hat’ 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 'hat'
it = wordMap.find("hat");

// Check if element exists in map or not
if (it != wordMap.end())
{
	// Element with key 'hat' found
	std::cout << "'hat' 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;
}

Complete example as follows,

#include <iostream>
#include <map>
#include <string>
#include <iterator>
#include <algorithm>

int main() {

	// Map of string & int i.e. words as key & there
	// occurrence count as values
	std::map<std::string, int> wordMap = {
			{ "is", 6 }, { "the", 5 },
			{ "hat", 9 }, { "at", 6 }
										};

	// Create an iterator of map
	std::map<std::string, int>::iterator it;

	// Find the element with key 'hat'
	it = wordMap.find("hat");

	// Check if element exists in map or not
	if (it != wordMap.end()) {
		// Element with key 'hat' found
		std::cout << "'hat' 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 = " << value << std::endl;
	} else {
		// Element with key 'hat' Not Found
		std::cout << "'hat' Not Found" << std::endl;
	}

	// Find the element with key 'hello'
	it = wordMap.find("hello");

	// Check if element exists in map or not
	if (it != wordMap.end()) {
		// Element with key 'hello' found
		std::cout << "'hello' 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;

	} else {
		// Element with key 'hello' Not Found
		std::cout << "'hello' Not Found" << std::endl;
	}

	return 0;
}

Output:

'hat' Found
key = hat :: Value = 9
'hello' Not Found

To Compile the above code use following command,

g++ –std=c++11 example.cpp

 

Cart

Your Cart is Empty

Back To Shop