Is there already some std::vector based set/map implementation?

前端 未结 6 1519
囚心锁ツ
囚心锁ツ 2021-02-08 07:43

For small sets or maps, it\'s usually much faster to just use a sorted vector, instead of the tree-based set/map - especially for something like 5-10 e

相关标签:
6条回答
  • 2021-02-08 08:29

    If you can't find anything suitable, I would just wrap a std::vector to do sort() on insert, and implement find() using lower_bound(). It should be straight forward, and just as efficient as a custom solution.

    0 讨论(0)
  • 2021-02-08 08:29

    Maybe you're looking for unordered map's and unordered set's. Try taking a look at the TR1 unordered containers that rely on hashing, or the Boost.Unordered container library. Underneath the interface, I'm not sure if they really do use std::vector, but I'd wager it's worth taking a look at.

    0 讨论(0)
  • 2021-02-08 08:36

    If the set or map truly is small, the performance gained by micro-optimizing the data structure will have little to no noticeable effects. You'll save maybe one or two memory (read: cache) lookups when searching a tiny tree vs tiny vector, which in the big picture is insignificant.

    Having said that, you could give hash_map a try. Lookups by key are guaranteed to run in constant time.

    0 讨论(0)
  • 2021-02-08 08:40

    I don't know any such implementation, but there are some functions that help working with sorted vectors already in STL, such as lower_bound and upper_bound.

    0 讨论(0)
  • 2021-02-08 08:43

    Old post, I know, but for more recent visitors, Boost's flat_set and flat_map look like what you need. See https://theboostcpplibraries.com/boost.container for more information.

    0 讨论(0)
  • 2021-02-08 08:44

    I just stumbled upon your question, hope its not too late.

    I recommend a great (open source) library named Loki. It has a vector based implementation of an associative container that is a drop-in replacement for std::map, called AssocVector.

    It offers better performance for accessing elements (and worst performance for insertions/deletions).

    The library was written by Andrei Alexandrescu author of Modern C++ Design.

    It also contains some other really nifty stuff.

    0 讨论(0)
提交回复
热议问题