Check if a key is present in a C++ map or unordered_map Last Updated : 30 Apr, 2024 Comments Improve Suggest changes Like Article Like Report A C++ map and unordered_map are initialized to some keys and their respective mapped values. Examples: Input : Map : 1 -> 4, 2 -> 6, 4 -> 6Check1 : 5, Check2 : 4Output : 5 : Not present, 4 : PresentC++ implementation : 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 not string check_key(map<int, int> m, int key) { // Key is not present if (m.find(key) == m.end()) return "Not Present"; return "Present"; } // Driver int 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); } unordered_map // 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 string check_key(unordered_map<int, int> m, int key) { // Key is not present if (m.find(key) == m.end()) return "Not Present"; return "Present"; } // Driver int 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 Present4: PresentApproach 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"; } // Driver int 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"; } // Driver int 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 Comment More infoAdvertise with us Next Article Check if a key is present in a C++ map or unordered_map R Rohit Thapliyal Improve Article Tags : C++ DSA STL cpp-unordered_map cpp-map cpp-unordered_map-functions +2 More Practice Tags : CPPSTL Similar Reads unordered_map key_eq() function in C++ STL unordered_map::key_eq() is a built-in function in C++ STL which returns a boolean value according to the comparison. It depends on the key equivalence comparison predicate used by the unordered_map container. The key equivalence comparison is a predicate which takes two arguments and returns a boole 2 min read unordered_multimap key_eq() function in C++ STL unordered_multimap::key_eq() is a built-in function in C++ STL which returns a boolean value according to the comparison. It depends on the key equivalence comparison predicate used by the unordered_multimap container. The key equivalence comparison is a predicate which takes two arguments and retur 2 min read How to create an unordered_map of pairs in C++? Unordered Map does not contain a hash function for a pair like it has for int, string, etc, So if we want to hash a pair then we have to explicitly provide it with a hash function that can hash a pair. unordered_map can takes upto 5 arguments: Key : Type of key valuesValue : Type of value to be stor 3 min read How to create an unordered_map of tuples in C++? Tuple - A tuple is an object that can hold a number of elements. The elements can be of different data types. The elements of tuples are initialized as arguments in the order in which they will be accessed. Unordered Map does not contain a hash function for a tuple. So if we want to hash a tuple the 2 min read unordered_map insert in C++ STL The std::unordered_map::insert() in C++ STL is a built-in function used to insert a key-value pair in unordered_map container. As unordered maps only store unique elements, this function does not insert elements with duplicate keys. In this article, we will learn about std::unordered_map::insert() i 4 min read Like