Check if a given key exists in a map or not in C++
This post will discuss how to check if a given key exists in a map or not in C++.
1. Using unordered_map::find function
To check for the existence of a particular key in the map, the standard solution is to use the public member function find() of the ordered or the unordered map container, which returns an iterator to the key-value pair if the specified key is found, or iterator to the end of the container if the specified key is not found.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <iostream> #include <unordered_map> #include <algorithm> int main() { std::unordered_map<char, int> m; std::string s("abcba"); 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 not found
2. Using unordered_map::count function
If we only want to know the presence of a key in the map container but doesn’t want an iterator to it, we can use the count() member function of the map container, which returns the value of 1 if the specified key is found, or 0 if the key is not found. Since all the keys in a map are distinct, count() internally uses find() function.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <iostream> #include <unordered_map> #include <algorithm> int main() { std::unordered_map<char, int> m; std::string s("abcba"); 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 found
3. Using STL algorithms
There are many algorithms offered by the standard library like std::find_if, std::count_if, std::for_each, std::any_of, etc., which can be used to linearly searches the map container for a key.
⮚ std::find_if function
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#include <iostream> #include <unordered_map> #include <algorithm> int main() { std::unordered_map<char, int> m; std::string s("abcba"); 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 found
⮚ std::count_if function
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#include <iostream> #include <unordered_map> #include <algorithm> int main() { std::unordered_map<char, int> m; std::string s("abcba"); 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 not found
⮚ std::for_each function
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
#include <iostream> #include <unordered_map> #include <algorithm> int main() { std::unordered_map<char, int> m; std::string s("abcba"); 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 found
⮚ std::any_of function
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#include <iostream> #include <unordered_map> #include <algorithm> int main() { std::unordered_map<char, int> m; std::string s("abcba"); 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 found
That’s all about determining whether a given key exists in a map or not in C++.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)