C++ set: counting elements less than a value

后端 未结 3 1139
臣服心动
臣服心动 2020-12-24 12:26

Assuming a I have an STL set s and an int x, how can I count the number of elements in s that are less than x

3条回答
  •  滥情空心
    2020-12-24 12:46

    What you need is an 'order-statistics tree'. It is essentially an augmented (binary search) tree that supports the additional operation rank(x) which gives you the number of elements with less or equal key as element x. Chapter 14 in Cormen, Leiserson, Rivest, Stein; "Introduction to Algorithms" should give you the algorithmic background.

    There is also some implementation on the web.

提交回复
热议问题