stl - How can I check values in a C++ map without getting compiler errors in a "const" member function? -


i have member function compares data in c++ map. declared member function constant:

bool operator == (unitset otherset) const; 

but use mymap["l"] access , compare values compiler errors mymap being constant. there way around while keeping const directive?

i know empty value created "l" key if didn't exist when check it, won't change value / behaviour of unitset - maybe i'm trying picky here though , should drop "const". way go this?

thanks.

std::map unfortunately doesn't have const overload of [] operator. because whenever access key doesn't exist, map creates element it; if map const, impossible. therefore, there's no way call constant map. isn't related behavior of unitset class.

you'll have use mymap.find(tkey) iterator element, returns mymap.end() if can't find it.

map iterators point std::pair of key , value, need access second member of value (first being key).

const unitset& reference = mymap.find("l")->second; 

Comments