How can I increase the performance in a map lookup with key type std::string?

后端 未结 14 993
天涯浪人
天涯浪人 2021-02-05 23:25

I\'m using a std::map (VC++ implementation) and it\'s a little slow for lookups via the map\'s find method.

The key type is std::string.

14条回答
  •  心在旅途
    2021-02-05 23:56

    Try std::tr1::unordered_map (found in the header ). This is a hash map, and, while it doesn't maintain a sorted order of elements, will likely be far faster than a regular map.

    If your compiler doesn't support TR1, get a newer version. MSVC and gcc both support TR1, and I believe the newest versions of most other compilers also have support. Unfortunately, a lot of the library reference sites haven't been updated, so TR1 remains a largely-unknown piece of technology.

    I hope C++0x isn't the same way.

    EDIT: Note that the default hashing method for tr1::unordered_map is tr1::hash, which needs to be specialized to work on a UDT, probably.

提交回复
热议问题