There is a upper_bound()
, returns an iterator to the first element that is greater than val.
There is a lower_bound()
, returns an iterator to the first element that is not less than val.
Is there an algorithm that returns an iterator to the first element that is not greater than val, or I have to reinvent the wheel?
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main()
{
std::vector<int> data = { 1, 1, 2, 3, 3, 3, 3, 5, 5, 6 };
auto lower = std::lower_bound(data.begin(), data.end(), 4, [](int x, int y) {return x > y;});
cout << *lower ;
}
Output: 1, expexted 3
Note that another predicate like std::greater<>
doesn't work.