c++ - Does std::map support caching? -
for example:
code1:
if((iter = map.find(key)) != map.end()) { return iter->second; } return 0;
code2:
if(map.count(key) > 0) { return map.at(key); } return 0;
code2 more simpler, both map.count()
, map.at()
cost o(logn) time. std::map
provide feature stores last search item in cache , make searching same item faster, or perform second search in whole map?
it search through whole map, there no caching being done - or @ least, standard not mandate , no implementation it, because all clients of such implementation have pay possibly undesired overhead of updating cached information after each insertion/removal.
the first approach idiomatic way determine whether key/value pair contained in map (just mind fact operator ->
should used instead of operator .
, since find()
iterator, , assignment iter
should outside if
condition):
auto iter = map.find(key); if (iter != map.end()) { return iter->second; }
Comments
Post a Comment