Check if a key is present in a C++ map or unordered_map Last Updated : 30 Apr, 2024 Comments Improve Suggest changes 29 Likes 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 Create Quiz Comment R Rohit Thapliyal 29 Improve R Rohit Thapliyal 29 Improve Article Tags : C++ STL cpp-unordered_map cpp-map cpp-unordered_map-functions +1 More Explore C++ BasicsIntroduction to C++3 min readData Types in C++6 min readVariables in C++4 min readOperators in C++9 min readBasic Input / Output in C++3 min readControl flow statements in Programming15+ min readLoops in C++7 min readFunctions in C++8 min readArrays in C++8 min readCore ConceptsPointers and References in C++5 min readnew and delete Operators in C++ For Dynamic Memory5 min readTemplates in C++8 min readStructures, Unions and Enumerations in C++3 min readException Handling in C++12 min readFile Handling in C++8 min readMultithreading in C++8 min readNamespace in C++5 min readOOP in C++Object Oriented Programming in C++8 min readInheritance in C++6 min readPolymorphism in C++5 min readEncapsulation in C++3 min readAbstraction in C++4 min readStandard Template Library(STL)Standard Template Library (STL) in C++3 min readContainers in C++ STL2 min readIterators in C++ STL10 min readC++ STL Algorithm Library3 min readPractice & ProblemsC++ Interview Questions and Answers1 min readC++ Programming Examples4 min read Like