Searching in a Map Using std::map Functions in C++
Last Updated :
18 Nov, 2024
In C++, map container is defined as std::map class template that also contains member function to search for an element on the bases of the keys. In this article, we will learn different methods to search for an element with the given key in C++.
The recommended method to search for the given key in a map container is by using map find() function. Let’s take a look at a simple example:
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
map<char, int> m = {{'a', 1}, {'c', 3},
{'b', 4}, {'d', 2}};
char key = 'b';
// Search the key
auto it = m.find(key);
// Checking if key present or not
if (it != m.end())
cout << it->second;
else
cout << key << " is NOT Present";
return 0;
}
Explanation: The map find() function returns an iterator to the given key, if it presents otherwise return an map end() iterator. So we compare it with map end() to check if the element exists or not.
std::map class provides some more methods to efficiently search for an element with the given key. They are as follows:
Using Map lower_bound()
The map lower_bound() function returns an iterator to the key which is just greater or equal to the given key. So, if an element with the given key is present, this function will return an iterator to it.
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
map<char, int> m = {{'a', 1}, {'c', 3},
{'b', 4}, {'d', 2}};
char key = 'b';
// Search the given key
auto it = m.lower_bound(key);
// Checking if key present or not
if (it != m.end() && it->first == key)
cout << it->second;
else
cout << "NOT Present";
return 0;
}
Explanation: As lower_bound() function returns an iterator to key whose value is just greater or equal to the given key, so to check whether the given key is present or not, we have to compare the value of searched key and given key.
Using Map upper_bound()
The map upper_bound() method returns an iterator to the key which is just greater than given key. If the element with the given key exists, it will be present just before this element pointed by this iterator.
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
map<char, int> m = {{'a', 1}, {'c', 3},
{'b', 4}, {'d', 2}};
char key = 'b';
// Search the given key
auto it = m.upper_bound(key);
it--;
// Checking if key present or not
if (it->first == key)
cout << it->second;
else
cout << "NOT Present";
return 0;
}
Explanation: The iterator returned by map upper_bound() is decremented to point to the element one place before. (possible matched element).
Using Map equal_range()
The map equal_range() method returns an iterator of pair, where pair first points to lower_bound() of the given key, and pair second points to upper_bound() of the given key. If key is not present, both pair first and pair second points to the next greater element.
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
map<char, int> m = {{'a', 1}, {'c', 3},
{'b', 4}, {'d', 2}};
char key = 'b';
// Search the given key
auto it = m.equal_range(key);
// Checking given key is Present or not
if (it.first != m.end() && it.first->first == key)
cout << it.first->second;
else
cout << key << " is NOT Present";
return 0;
}
Explanation: We compared the key of pair first with the given key to check whether the key is present or not.
Similar Reads
Search by value in a Map in C++ Given a set of N pairs as a (key, value) pairs in a map and an integer K, the task is to find all the keys mapped to the given value K. If there is no key value mapped to K then print "-1".Examples: Input: Map[] = { {1, 3}, {2, 3}, {4, -1}, {7, 2}, {10, 3} }, K = 3 Output: 1 2 10 Explanation: The 3
2 min read
map count() Function in C++ STL The std::map::count() in C++ is a built-in function that is used to count the occurrence of the given key in the map container. It is the member function of the std::map container.In this article, we will learn how to use std::map::count() in C++.Syntaxmp.count(key)Parameters key: The value whose oc
2 min read
map key_comp() function in C++ STL The map::key_comp() is a function in STL in C++ that returns a copy of comparison object used by container that compare keys. Syntax: map.key_comp() Return value: This method returns the comparison object used by container that compare keys. Below examples illustrate the working of key_comp() method
2 min read
unordered_map hash_function() function in C++ STL The unordered_map::hash_function() is a built in function in C++ STL which is used to get the hash function. This hash function is a unary function which takes a single argument only and returns a unique value of type size_t based on it. Syntax: unordered_map_name.hash_function() Parameter: The func
2 min read
unordered_multimap hash_function() in C++ STL The unordered_multimap::hash_function() is a built-in function in C++ STL which is used to get the hash function. This hash function is a unary function which takes a single argument only and returns a unique value of type size_t based on it. Syntax: unordered_multimap_name.hash_function() Parameter
2 min read
Traversing a Map and unordered_map in C++ STL The maps are described as mapped associative containers for elements where each element has a key and value assigned to it. Another form of map container seen in the C++ STL is the unordered map. It is the same as map containers just that they don't store the data in sorted order.We can traverse map
5 min read