Quantcast
Channel: How to find if a given key exists in a std::map - Stack Overflow
Viewing all articles
Browse latest Browse all 16

Answer by hustljian for How to find if a given key exists in a std::map

$
0
0
map<string, string> m;

check key exist or not, and return number of occurs(0/1 in map):

int num = m.count("f");  if (num>0) {        //found   } else {      // not found  }

check key exist or not, and return iterator:

map<string,string>::iterator mi = m.find("f");  if(mi != m.end()) {      //found      //do something to mi.  } else {      // not found  }  

in your question, the error caused by bad operator<< overload, because p.first is map<string, string>, you can not print it out. try this:

if(p.first != p.second) {    cout << p.first->first << ""<< p.first->second << endl;}

Viewing all articles
Browse latest Browse all 16

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>