How to Check if Map Contains a Specific Key in C++?
Last Updated :
21 Oct, 2024
In C++, std::map is an STL container that stores key-value pairs in a sorted order based on the keys. In this article, we will learn how to check if an element with the given key exists in a map or not in C++.
Examples
Input: m = {{5, "apple"}, {4, "kiwi"}, {6, "cherry"}}, k = 4
Output: Key found.
Explanation: The element with key 4 is present in the map.
Input: m = {{5, "apple"}, {4, "kiwi"}, {6, "cherry"}}, k = 8
Output: Key NOT found.
Explanation: The element with key 8 is NOT present in the map.
Following are the 4 different methods to check if the std::map contains the element with specific key or not in C++:
Using map::find() Method
C++ STL provides the std::map::find() method that searches the map for a given key and returns an iterator to the element if it is found. If it is not found, std::map::end() is returned. It is a member function of std::map, so we can directly use it using dot operator.
Example
C++
// C++ program to check if a map contains
// a specific key using map::find() method
#include <bits/stdc++.h>
using namespace std;
int main() {
map<int, string> m = {{5, "apple"},
{4,"kiwi"}, {6, "cherry"}};
int k = 4;
// Check if the key is found
if (m.find(k) != m.end()) {
cout << "Key found";
} else {
cout << "Key not found";
}
return 0;
}
Time Complexity: O(log n), where n is the number of elements in the map.
Auxiliary Space: O(1)
Using map::count() Method
We can also use the std::map::count() method to check whether the map contains a specific key. If the key is present, it returns 1, otherwise, it returns 0. It is also a member function of std::map.
Example
C++
// C++ program to check if a map contains
// a specific key using map::count() method
#include <bits/stdc++.h>
using namespace std;
int main() {
map<int, string> m = {{5, "apple"},
{4, "kiwi"}, {6, "cherry"}};
int k = 4;
// Check if the key is found
if (m.count(k) > 0) {
cout << "Key found";
} else {
cout << "Key not found";
}
return 0;
}
Time Complexity: O(log n), where n is the number of elements in the map.
Auxiliary Space: O(1)
Using map::contains() Method (C++ 20)
Since C++20, we can also use the std::map::contains() method to check whether the map contains a specific key. If the key is present, it returns true, otherwise, it returns false.
Example
C++
// C++ program to check if a map contains a
// specific key using map::contains()
#include <bits/stdc++.h>
using namespace std;
int main() {
map<int, string> m = {{5, "apple"},
{4, "kiwi"}, {6, "cherry"}};
int k = 4;
// Check if key is found
if (m.contains(k)) {
cout << "Key found";
} else {
cout << "Key not found";
}
return 0;
}
Output
Key found
Time Complexity: O(log n), where n is the number of elements in the map.
Auxiliary Space: O(1)
Manually Using Loop
Another way to check if a map contains a specific key is by manually iterating through the std::map container using a loop with iterators. We compare each key of the map with the target key inside an if statement. If the key is found, set the flag as true and return
Example
C++
// C++ program to check if a map contains
// a specific key using a manual loop
#include <bits/stdc++.h>
using namespace std;
bool isKeyPresent(map<int, string>& m, int k) {
// Manually iterate over map to find key
for (auto &p : m) {
// If key is found
if (p.first == k) {
return true;
}
}
// If we reach the map end without finding
// the key
return false;
}
int main() {
map<int, string> m = {{5, "apple"},
{4, "kiwi"}, {6, "cherry"}};
int k = 4;
// Check if key exists
if (isKeyPresent(m, k)) {
cout << "Key found";
} else {
cout << "Key not found";
}
return 0;
}
Time Complexity: O(n), where n is the number of elements in the map.
Auxiliary Space: O(1)
Similar Reads
How to Check if a Map is Empty in C++? In C++, a map is an associative container that stores elements as key-value pairs and an empty map means it contains no elements. In this article, we will learn how to check if a map is empty or not in C++. Example: Input: map<int,string>mp1 = {{1, "Ram"}, {2, "Mohit"}};map<int,string> m
2 min read
How to Find the Minimum Key in a Map in C++? In C++, a map is an associative container that stores key-value pairs. In this article, we will learn how to find the minimum key in a map in C++. Example: Input: myMap= {{10,"apple"},{5,"banana"},{20,"cherry"}};Output:Minimum key: 5Find the Smallest Key in a Map in C++There is no direct method avai
2 min read
How to Find First Key-Value Pair in a Map in C++? In C++ STL, a map is a container that stores key-value pairs in an ordered or sorted manner. In this article, we will learn how to find the first key-value pair in a Map. Example: Input: myMap = {{1, "C++"}, {2, "Java"}, {3, "Python"}, {4, "JavaScript"}}; Output: First key-Value Pair : (1, C++)Getti
2 min read
How to Find the Maximum Key in a Map in C++? In C++, the Standard Template Library (STL) provides a map container to store elements in a mapped fashion so each element has a key value and a mapped value. In this article, we will see how to find the maximum key in a map in C++. Example: Input : myMap : {{1, 10}, {2, 20}, {4, 12}, {3, 44}}Output
2 min read
How To Find All Occurrences of a Key in a Multimap in C++? In C++, multimaps are associative containers similar to maps, but unlike maps, they can store multiple values mapped to the same key. In this article, we will learn how to find all the occurrences of a specific key in a multimap in C++. Example: Input:myMutimap = {{ "id", "111" }, { "id", "112" }, {
2 min read
How to Find Frequency of a Key in a Multimap in C++? In C++, Multimap is similar to a map that stores the data in the key-value format but the difference between these two containers is that we can have multiple elements with the same keys. In this article, we will learn how to find the frequency of a specific key in a multimap in C++. Example Input:
2 min read