How to Use the std::map::find Function in C++
-
Use the
std::map::find
Function to Find the Element With a Given Key Value in C++ -
Use the
contains
Member Function to Check if the Given Element Exists in a Map in C++
This article explains how to utilize the std::map::find
function and some of its alternatives in C++.
Use the std::map::find
Function to Find the Element With a Given Key Value in C++
The std::map
object is one of the associative containers in the C++ standard template library, and it implements a sorted data structure, storing key values. Note that keys are unique in the std::map
container. Thus, if new elements are inserted with the existing keys, the operation does not have any effect. Still, some special member functions in the std::map
class can assign new values to the existing pairs if the keys are matched (e.g. insert_or_assign
function).
The advantages of the std::map
container include fast search, insertion, and removal operations, which can be conducted in logarithmic time. The search operation is provided by the find
member, which accepts a reference to a key. If the given key is found in the std::map
object, the iterator to the corresponding element is returned. On the other hand, suppose the given key is not found in the container, the past-the-end iterator (map::end()
) is returned.
The following code snippet demonstrates a simple usage scenario where a map
container of string
pairs is initialized with arbitrary values, and then the key with the "h"
value is searched. Consequently, we process the returned iterator with the if-else
statement to either print the element pair or output a message that the given key was not found.
#include <iostream>
#include <map>
using std::cin;
using std::cout;
using std::endl;
using std::map;
using std::string;
int main() {
std::map<string, string> m1 = {
{"h", "htop"}, {"k", "ktop"}, {"t", "ttop"},
{"r", "rtop"}, {"w", "wtop"}, {"p", "ptop"},
};
string key = "h";
auto item = m1.find(key);
if (item != m1.end()) {
cout << "Key exists! - {" << item->first << ";" << item->second << "}\n";
} else {
cout << "Key does not exist!" << endl;
}
return EXIT_SUCCESS;
}
Output:
Key exists! - {h;htop}
Use the contains
Member Function to Check if the Given Element Exists in a Map in C++
If the user needs to confirm if the pair with the given value exists in the map
object, one can utilize the member function contains
. The function has been part of the std::map
container since the C++20 version, so you should know the compiler version to run the following code snippet. The contains
function takes a reference to the key and returns the bool
value true
if it’s found. The time complexity of this member function is also logarithmic.
#include <iostream>
#include <map>
using std::cin;
using std::cout;
using std::endl;
using std::map;
using std::string;
int main() {
std::map<string, string> m1 = {
{"h", "htop"}, {"k", "ktop"}, {"t", "ttop"},
{"r", "rtop"}, {"w", "wtop"}, {"p", "ptop"},
};
string key = "h";
if (m1.contains(key)) {
cout << "Key Exists!" << endl;
} else {
cout << "Key does not exist!" << endl;
}
return EXIT_SUCCESS;
}
Output:
Key Exists!
Alternatively, one can use the cout
member function to check if the element pair with the given key exists in the map
. Generally, the cout
function is utilized to retrieve the number of elements with the given key, but since the std::map
stores only unique key values, the process returns 1
if the element is found. Otherwise, a zero value is returned to indicate that no element was found.
#include <iostream>
#include <map>
using std::cin;
using std::cout;
using std::endl;
using std::map;
using std::string;
int main() {
std::map<string, string> m1 = {
{"h", "htop"}, {"k", "ktop"}, {"t", "ttop"},
{"r", "rtop"}, {"w", "wtop"}, {"p", "ptop"},
};
string key = "h";
if (m1.count(key)) {
cout << "Key Exists!" << endl;
} else {
cout << "Key does not exist!" << endl;
}
return EXIT_SUCCESS;
}
Output:
Key Exists!
Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.
LinkedIn Facebook