set vs unordered_set for fastest iteration

前端 未结 5 1604
无人及你
无人及你 2021-02-13 02:18

In my application I have the following requirements -

  1. The data structure will be populated just once with some values (not key/value pairs). The values may be r

5条回答
  •  情深已故
    2021-02-13 02:51

    If building of the data-structure does not factor into the performance-concerns (or at least only marginally), consider saving your data into a std::vector: There's nothing beating it.

    For speeding up the initial building of the data-structure, you might first insert into a std::unordered_set or at least use one for checking existence before insertion.

    In the second case it need not contain the elements, but could contain e.g. indices.

    std::vector v;
    auto h = [&v](size_t i){return std::hash()(v[i]);};
    auto c = [&v](size_t a, size_t b){return v[a] == v[b];};
    std::unordered_set tester(0, h, c);
    

提交回复
热议问题