Search code examples
c++setstdset

C++ set -- number of elements with a key less than x


I have a set<int> and I want to see how many elements in it are less than x. (x is also int)

What should i do?


Solution

  • Use lower_bound to compute std::distance(s.begin(), s.lower_bound(x)). (If x is a key, this counts the number of elements strictly before x.)