Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if a key is present in a C++ map or unordered_map
In C++ the Maps and unordered maps are hash tables. They use some keys and their respective key values. Here we will see how to check whether a given key is present in the hash table or not. The code will be like below −
Example
#include<iostream>
#include<map>
using namespace std;
string isPresent(map<string, int> m, string key) {
if (m.find(key) == m.end())
return "Not Present";
return "Present";
}
int main() {
map<string, int> my_map;
my_map["first"] = 4;
my_map["second"] = 6;
my_map["third"] = 6;
string check1 = "fifth", check2 = "third";
cout << check1 << ": " << isPresent(my_map, check1) << endl;
cout << check2 << ": " << isPresent(my_map, check2);
}
Output
fifth: Not Present third: Present
Advertisements