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 Set Contains an Element in C++? In C++, the std::set container stores the unique elements in the sorted order. In this article, we will learn how to check if the set contains a given element or not.ExamplesInput: s = {1, 3, 5, 7, 9}, x = 5Output: Element foundExplanation: The element 5 is present in the set.Input: s = {10, 20, 30,
4 min read
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 Check if a Vector Contains a Given Element in C++? In C++, a vector is a dynamic array that provides a resizable and efficient way to store elements. Vectors store their elements in contiguous memory locations, similar to arrays, which allows for efficient access and iteration.In this article, we will learn the different methods to check whether the
3 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