stl - C++ - how to tell whether a key exists in map of maps -
i have following structure:
std::map<int, std::map<int, int>> my_map;
i want check whether key my_map[3][5]
exists.
is there easier/shorter way except like:
if (my_map.find(3) != my_map.end()) { std::map<int, int>& internal_map = my_map[3]; if (internal_map.find(5) != internal_map.end()) { // ... ... } }
you improve little via:
std::map<int, std::map<int, int>>::iterator = my_map.find(3); if (it != my_map.end()){ if (it->second.find(5) != internal_map.end()) { } }
in current solution, finding key twice , slower finding once , store in iterator.
Comments
Post a Comment