Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C++
  • Standard Template Library
  • STL Vector
  • STL List
  • STL Set
  • STL Map
  • STL Stack
  • STL Queue
  • STL Priority Queue
  • STL Interview Questions
  • STL Cheatsheet
  • C++ Templates
  • C++ Functors
  • C++ Iterators
Open In App
Next Article:
How to create an unordered_map of tuples in C++?
Next article icon

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 -> 6
Check1 : 5, Check2 : 4
Output : 5 : Not present, 4 : Present


C++ 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 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"; }  // 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


 



Next Article
How to create an unordered_map of tuples in C++?
https://media.geeksforgeeks.org/auth/avatar.png
GeeksforGeeks
Improve
Article Tags :
  • C++
  • cpp-map
  • cpp-unordered_map
  • cpp-unordered_map-functions
  • STL
Practice Tags :
  • CPP
  • STL

Similar Reads

  • Traversing a Map and unordered_map in C++ STL
    The maps are described as mapped associative containers for elements where each element has a key and value assigned to it. Another form of map container seen in the C++ STL is the unordered map. It is the same as map containers just that they don't store the data in sorted order. We can traverse ma
    6 min read
  • 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
    5 min read
  • unordered_map cbegin in C++ STL
    cbegin function in c++ is used to return a constant iterator pointing the first element in an unordered map. Syntax: unordered_map.cbegin() Parameter: It takes an optional parameter N. If set, the iterator returned will point to the first element of the bucket otherwise it point to the first element
    2 min read
  • unordered_map find in C++ STL
    In C++, std::unordered_map::find function is used to search for a specific element using the key in an unordered map container. It is a member function of std::unordered_map container defined inside <unordered_map> header file. Syntaxum.find(key); Parameterskey: The key of the element to be se
    1 min read
  • unordered_map operator[] in C++ STL
    The std::unordered_map::operator[] is a built in function in C++ STL which returns the reference of value if key matches in the container. If no key is found then it inserts that key into container. Syntax: mapped_type& operator[](key_type&& k); Parameter: It takes parameter as key whose
    2 min read
  • unordered_map cend in C++ STL
    The unordered_map::cend() is a built-in function in C++ STL which returns an iterator pointing to the position past the end element in the container or in one of its bucket. In an unordered_map object, there is no guarantee that which specific element is considered its first element. But all the ele
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences