• Register
100 points
7 2
Welcome to Kodlogs, programming questions and answer website.

Maps are containers that can store elements formed by a combination of key values and mapped values. Key values are used to sort and uniquely identify the elements and mapped values are the real content. We can assume key values such as address of houses and mapped values as the things in the house i.e., content.

 

Method to check if the key is available

Methods to check if the key exists in the program are:STL algorithms

  • Using unordered_map::find

  • Using unoredered_map::count

  • Using STL algorithms

 

Unordered_map::find

The standard and the easiest way to find a key is to use find(). Which is a public member function of ordered and unordered_map. The find() returns an iterator to the key-value if the key is found, or an iterator to the end of the container if key is not found.

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

int main()
{
	std::unordered_map<char,int> m;

	std::string s("Kodlogs");
	std::for_each(s.begin(), s.end(), [&m](char &c) { m[c]++; });

	char ch = 's';

	if (m.find(ch) != m.end()) {
		std::cout << "Key found";
	} else {
		std::cout << "Key not found";
	}

	return 0;
}

Output

key found

Unordered_map::count

But if we want to know if the key is present or not but do not want an iterator to it, so we can use count(). count() is the member function of the map container. It returns the value as 1 if the key is found, it returns the value to 0.

Fact: count() internally uses find() since all the keys are distincts.

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

int main()
{
	std::unordered_map<char,int> m;

	std::string s("kodlogs");
	std::for_each(s.begin(), s.end(), [&m](char &c) { m[c]++; });

	char ch = 'a';

	if (m.count(ch)) {
		std::cout << "Key found";
	} else {
		std::cout << "Key not found";
	}

	return 0;
}

Output

key not found

STL algorithms

For searching for a key in the map container, the standard library provides algorithms like std:find_if, std::cout_if, std::for_each and many more.

  1. std::find_if
#include <iostream>
#include <unordered_map>
#include <algorithm>

int main()
{
	std::unordered_map<char,int> m;

	std::string s("kodlogs");
	std::for_each(s.begin(), s.end(), [&m](char &c) { m[c]++; });

	char ch = 'a';

	auto it = std::find_if(m.begin(), m.end(),
				  [&ch](std::pair<const char,int> &entry){
						return (entry.first == ch);
				  });

	if (it != m.end()) {
		std::cout << "Key found";
	} else {
		std::cout << "Key not found";
	}

	return 0;
}

Output

 key not found
  1. std::count_if
#include <iostream>
#include <unordered_map>
#include <algorithm>

int main()
{
	std::unordered_map<char,int> m;

	std::string s("kodlogs");
	std::for_each(s.begin(), s.end(), [&m](char &c) { m[c]++; });

	char ch = 's';

	int count = std::count_if(m.begin(), m.end(),
				  [&ch](std::pair<const char,int> &entry){
						return (entry.first == ch);
				  });

	if (count) {
		std::cout << "Key found";
	} else {
		std::cout << "Key not found";
	}

	return 0;
}

Output

key found
  1. std::for_each
#include <iostream>
#include <unordered_map>
#include <algorithm>

int main()
{
	std::unordered_map<char,int> m;

	std::string s("kodlogs");
	std::for_each(s.begin(), s.end(), [&m](char &c) { m[c]++; });

	char ch = 'a';

	bool found = false;
	std::for_each(m.begin(), m.end(),
				  [&ch, &found](std::pair<const char,int> &entry) {
						if (entry.first == ch) {
							found = true;
						}
				  });

	if (found) {
		std::cout << "Key found";
	} else {
		std::cout << "Key not found";
	}

	return 0;
}

Output 

key not found
  1. std::any_of
#include <iostream>
#include <unordered_map>
#include <algorithm>

int main()
{
	std::unordered_map<char,int> m;

	std::string s("kodlogs");
	std::for_each(s.begin(), s.end(), [&m](char &c) { m[c]++; });

	char ch = 'a';

	bool found = std::any_of(m.begin(), m.end(),
					[&ch](std::pair<const char,int> &entry){
						return (entry.first == ch);
					});

	if (found) {
		std::cout << "Key found";
	} else {
		std::cout << "Key not found";
	}

	return 0;
}

Output

 key not found

Hope this helps.

100 points
7 2