Is there any boost/stl container which supports the following operation?

前端 未结 3 430
野性不改
野性不改 2021-01-22 01:50

I was looking for stl/boost container which can provide following functionality:

  1. Auto insert element in sorted order. (log n)
  2. Return the index/depth of el
3条回答
  •  生来不讨喜
    2021-01-22 02:23

    std::map and std::set acording to the standard guarantee O(log(N)) insertion and searching, they also satisfy the sorted order condition. Please see C++ standard at section 23.4.

    Update after @StefanoSanfilippo constructive comment:

    Have in mind though, that these containers allow only unique keys/elements. If you have multiple values you have to resort to std::multimap and std::multiset. These containers have almost the same properties with std::map and std::set but allow multiple keys/elements.

    Now about with index/depth issue as far as it concerns STL containers, it's not guaranteed that std::map and std::set are implemented as binary-trees and as such there is no interface for accessing tree properties such as depth and index (please see How to find the depth of each node in std::map?). Making an educated guess, I think that the same goes for boost's tree like containers.

    Update - Quoting from @Mooing Duck's comment:

    boost trees also do not have ways to get the index.

提交回复
热议问题