Count elements lower than a given value in a std::set
The correct way to do a lower bound search is with std::set
's own lower_bound
function, which is specially designed to work with this sorted, associative, non-random-access container.
So, instead of this:
std::lower_bound( mySet.begin(), mySet.end(), 2 );
use this:
mySet.lower_bound(2);
This is logarithmic in the size of the container, which is much better than a std::count_if
approach (which doesn't know about the sortyness of the comparator, and therefore must visit all the nodes and is thus linear).
However, you then must also use std::distance
from the beginning to the lower bound, which is not only linear but also necessarily "slow" in practice (due to the non-random-access).
Nathan's solution seems optimal given that you do not want to simply find the lower bound, but find its distance from the "start" of the container.
Looks like operator- is not valid for a std::set::iterator. Why?
Indeed, an implementation of std::set::iterator::operator-()
can't exist in constant complexity since the elements are not contiguous in memory.
Then, how can you easily (without calling std::previous or std::next until bound is reached...this would not be efficient) find the position of a given iterator in the container?
You can't, std::set::iterator
is not a RandomAccessIterator. See std::distance()
documentation:
Complexity
Linear.
If you can't, then what alterantive can I use to find the index of a given element...?
I'd suggest to count your elements without having to compute an iterator distance: std::count_if()
can help us:
#include <iostream>
#include <algorithm>
#include <set>
int main()
{
std::set<int> mySet;
mySet.insert( 1 );
mySet.insert( 2 );
mySet.insert( 3 );
mySet.insert( 4 );
const std::size_t lower_than_three = std::count_if(
std::begin(mySet)
, std::end(mySet)
, [](int elem){ return elem < 3; } );
std::cout << lower_than_three << std::endl;
}
Demo
Since std::set::iterator
is a BidirectionalIterator we cannot subtract from it unless we use the decrement operator. What we can do though is just walk the set and count the iterations until we reach a number bigger than what we are looking for.
std::set<int> mySet;
// fill values
int counter = 0;
for (auto it = mySet.begin(), *it < some_value && it != mySet.end(); ++it)
{
if (e < some_value)
counter++;
}
This is a worst mySet.size()
iterations which is as fast as you can get it when dealing with a BidirectionalIterator.
Also note that std::lower_bound
does not have O(log N) complexity since we are not using a RandomAccessIterator. When using a non-RandomAccessIterator it has linear complexity.