简体   繁体   中英

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 is much more simpler, but both map.count() and map.at() cost O(logn) time. Does std::map provide a feature that stores the last search item in cache and make searching same item faster, or does it just perform a second search in the whole map?

It does a search through the whole map, there is no caching being done - or at least, the Standard does not mandate any and I would say no implementation does it, because all clients of such an implementation would have to pay for the possibly undesired overhead of updating the cached information after each insertion/removal.

The first approach is the idiomatic way to determine whether a key/value pair is contained in a map (just mind the fact that operator -> should be used instead of operator . , since what you get from find() is an iterator, and the assignment to iter should be outside the if condition):

auto iter = map.find(key);
if (iter != map.end()) {
    return iter->second;
}

No, none of the C++ Standard Library implementations use caching, as far as I know. C++11 requires that containers are thread-safe for multiple readers. And in order to accomplish that the access to the cache need to be synchronized. That will result in a speed penalty, even though you don't want it. A standard practice of C++ is that you should not pay for anything you don't explicitly need or want.

It could, but none that I know of do. The idiomatic solution is thus to use a variable:

auto results = myMap.find( key );
return results == myMap.end()
    ? NULL
    : &results->second;

Short, clean and easily understandable. (And it avoids the multiple returns which make reasoning about the program correctness so difficult.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM