how to see if a key in a map

Started by
1 comment, last by Glak 16 years, 12 months ago

map<char,int> m;
m['a'] = 10;
m['b'] = 20;
m['c'] = 30;

pair<map<char,int>::iterator,map<char,int>::iterator> p = m.equal_range('c');

if(p.first == m.end() && p.second == m.end())  // or if(p.first==p.second==m.end())
    report_key_is_not_in_map();
I do this way, it seem a little complex, more simple way?
Advertisement
Hi

map<char,int> m;m['a'] = 10;m['b'] = 20;m['c'] = 30;if (m.find('c') == m.end()){    report_key_is_not_in_map();}


How about this?

my_map.count(key)

it returns the count as a number, which is implicitly converted to a bool

This topic is closed to new replies.

Advertisement