Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,924 questions

51,857 answers

573 users

How to check if a key exist in map in C++

2 Answers

0 votes
#include <iostream>
#include <string>
#include <map>
 
int main ()
{
    std::map<char, int> mp;
 
    mp['a'] = 1;
    mp['x'] = 7;
    mp['r'] = 9;
 
    if (mp.count('x') > 0)
      std::cout << "Found";
    else 
      std::cout << "Not Found";
    
    return 0;
}
 
 
 
/*
run:
 
Found
 
*/

 



answered Apr 13, 2020 by avibootz
edited Apr 13, 2020 by avibootz
0 votes
#include <iostream>
#include <string>
#include <map>
 
int main ()
{
    std::map<char, int> mp;
 
    mp['a'] = 1;
    mp['x'] = 7;
    mp['r'] = 9;
 
    std::map<char, int>::iterator it = mp.find('x');

    if (it == mp.end())
      std::cout << "Not Found";
    else 
      std::cout << "Found";
    
    return 0;
}
 
 
 
/*
run:
 
Found
 
*/

 



answered Apr 13, 2020 by avibootz

Related questions

4 answers 613 views
1 answer 222 views
1 answer 211 views
1 answer 153 views
1 answer 160 views
...