Check if a key is present in a C++ map or unordered_map - GeeksforGeeks

11 Oct, 2021
A C++ map and unordered_map are initialized to some keys and their respective mapped values. Examples:  
Input : 
Map : 1 -> 4, 2 -> 6, 4 -> 6
Check1 : 5, Check2 : 4
Output : 5 : Not present, 4 : Present
C++ implementation :  
map
unordered_map
// CPP code to check if a// key is present in a map#include <bits/stdc++.h>using namespace std; // Function to check if the key is present or notstring check_key(map<int, int> m, int key){    // Key is not present    if (m.find(key) == m.end())        return "Not Present";     return "Present";} // Driverint main(){    map<int, int> m;     // Initializing keys and mapped values    m[1] = 4;    m[2] = 6;    m[4] = 6;     // Keys to be checked    int check1 = 5, check2 = 4;     // Function call    cout << check1 << ": " << check_key(m, check1) << '\n';    cout << check2 << ": " << check_key(m, check2);}
// CPP code to check if a key is present// in an unordered_map#include <bits/stdc++.h>using namespace std; // Function to check if the key is present or notstring check_key(unordered_map<int, int> m, int key){    // Key is not present    if (m.find(key) == m.end())        return "Not Present";     return "Present";} // Driverint main(){    unordered_map<int, int> m;     // Initialising keys and mapped values    m[1] = 4;    m[2] = 6;    m[4] = 6;     // Keys to be checked    int check1 = 5, check2 = 4;     // Function call    cout << check1 << ": " << check_key(m, check1) << '\n';    cout << check2 << ": " << check_key(m, check2);}
Output:  
5: Not Present
4: Present
Approach 2nd: 
we can also use the count function of the map in c++.
Implementation:
1. Map
C++
// CPP code to check if a// key is present in a map#include <bits/stdc++.h>using namespace std; // Function to check if the key is present or not using count()string check_key(map<int, int> m, int key){    // Key is not present    if (m.count(key) == 0)        return "Not Present";     return "Present";} // Driverint main(){    map<int, int> m;     // Initializing keys and mapped values    m[1] = 4;    m[2] = 6;    m[4] = 6;     // Keys to be checked    int check1 = 5, check2 = 4;     // Function call    cout << check1 << ": " << check_key(m, check1) << '\n';    cout << check2 << ": " << check_key(m, check2);}
Output:
5: Not Present
4: Present
2. Unordered Map
C++
// CPP code to check if a key is present// in an unordered_map#include <bits/stdc++.h>using namespace std; // Function to check if the key is present or not using count()string check_key(unordered_map<int, int> m, int key){    // Key is not present    if (m.count(key) == 0)        return "Not Present";     return "Present";} // Driverint main(){    unordered_map<int, int> m;     // Initialising keys and mapped values    m[1] = 4;    m[2] = 6;    m[4] = 6;     // Keys to be checked    int check1 = 5, check2 = 4;     // Function call    cout << check1 << ": " << check_key(m, check1) << '\n';    cout << check2 << ": " << check_key(m, check2);}
Output:
5: Not Present
4: Present
This article is contributed by Rohit Thapliyal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
preethibr1
CoderSaty
cpp-map
cpp-unordered_map
cpp-unordered_map-functions
STL
C++
STL
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Inheritance in C++
C++ Classes and Objects
Bitwise Operators in C/C++
Operator Overloading in C++
Socket Programming in C/C++
Constructors in C++
Virtual Function in C++
Multidimensional Arrays in C / C++
Templates in C++ with Examples
Copy Constructor in C++