. Advertisement .
..3..
. Advertisement .
..4..
Are you looking for a way to check if key exist in C++ map? You are in luck. This article provides not only one but three different approaches to perform this task. Check it out and learn now!
Method 1: Check If Key Exist In C++ Map Utilizing std::map::find
The container std::map
is a type of associative structure containing sorted key value pairs, where each component comes with a different key. STL (standard template library) also offers an unsorted variant of this container, known as std::unordered_map
. These containers can both be used with the key finding techniques.
std::map
’s built-in function find accepts a single parameter of the key value to look for. This function gives back an iterator to the component with the specified key value; if not, it returns the past-the-end iterator.
As you can see in the sample below, std::pair<string, string>
types’ map is initialized. We also take the user input’s key value passed to the function find. Here, the affirmative string is output to the stream cout.
#include <iostream>
#include <map>
using std::cout; using std::cin;
using std::endl; using std::map;
using std::string;
int main(){
string key_to_find;
std::map<string, string> lang_map = {{"j", "Julia",},
{"p", "Python",},
{"m", "MATLAB",},
{"o", "Octave",},
{"s", "Scala",},
{"l", "Lua",}};
for (const auto & [key, value] : lang_map) {
cout << key << " : " << value << endl;
}
cout << "Enter the key to search for: ";
cin >> key_to_find;
if (lang_map.find(key_to_find) != lang_map.end()) {
cout << "Key Exists!" << endl;
}
return EXIT_SUCCESS;
}
Output:
j : Julia
l : Lua
m : MATLAB
o : Octave
p : Python
s : Scala
Enter the key to search for: l
Key Exists!
Method 2: Check If Key Exist In C++ Map Utilizing std::map::count
You can also use std::map
’s built-in feature count to examine if the provided key is present in the map object. Notice that this feature returns how many elements have the specified key value. In case there is no element, the key value 0 will be returned.
Therefore, you may utilize count as a condition if to print an affirming string when the specified key is present in the map object.
#include <iostream>
#include <map>
using std::cout; using std::cin;
using std::endl; using std::map;
using std::string;
int main(){
string key_to_find;
std::map<string, string> lang_map = {{"j", "Julia",},
{"p", "Python",},
{"m", "MATLAB",},
{"o", "Octave",},
{"s", "Scala",},
{"l", "Lua",}};
cout << "Enter the key to search for: ";
cin >> key_to_find;
if (lang_map.count(key_to_find)) {
cout << "Key Exists!" << endl;
}
return EXIT_SUCCESS;
}
Output:
Enter the key to search for: l
Key Exists!
Method 3: Check If Key Exist In C++ Map Utilizing std::map::contains
Another function you can utilize to check whether a key present in map is contains. If the element corresponding to the provided key is present in map, contains gives back a boolean value. Notably, the complexity of all three of the functions given in the post is logarithmic.
#include <iostream>
#include <map>
using std::cout; using std::cin;
using std::endl; using std::map;
using std::string;
int main(){
string key_to_find;
std::map<string, string> lang_map = {{"j", "Julia",},
{"p", "Python",},
{"m", "MATLAB",},
{"o", "Octave",},
{"s", "Scala",},
{"l", "Lua",}};
cout << "Enter the key to search for: ";
cin >> key_to_find;
if (lang_map.contains(key_to_find)) {
cout << "Key Exists!" << endl;
}
return EXIT_SUCCESS;
}
Output:
Enter the key to search for: l
Key Exists!
The Bottom Line
Above are the methods you can use to check if key exist in C++ map. All of these approaches are quite simple to execute. We recommend you try them all to see which one works best for you.Bonus: If you want to learn more techniques in C++, such as using the timer feature, you can take a look at this tutorial.
Leave a comment