When I want to make sure that the entry I want to use exists, I usually do this.
#include <unordered_map>
struct type { int member; };
std::unordered_map<type> map;
if (map.find(key) != map.end())
map[key].member = 42;
However, I think it performs two lookups for key
in the hash map. This caches the lookup.
#include <unordered_map>
struct type { int member; };
std::unordered_map<type> map;
auto find = map.find(key);
if (find != map.end())
find->second.member = 42;
The first option feels way more expressive. Is it really slower?