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

前端 未结 3 400
野性不改
野性不改 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:21

    A binary tree (B-tree) has logarithmic insertion and retrieval (equivalent to depth count) time in the average case and is naturally sorted when walked in-order.

    Unfortunately, a B-tree insertion/retrieval time might degenerate to linear if the tree is unbalanced (worst case). If this is an issue, you should consider a Red-Black tree, which does not eliminate the problem, but mitigates it while keeping the insertion time logarithmic in the size of the tree.

    Neither STL nor Boost implement a B- or RB- tree, although std::map is usually implemented as a RB.

    Please keep in mind that a linked list has linear insertion time, even if double.

提交回复
热议问题