You have to use the strict less operator From the C++ 2017 Standard (28.7 Sorting and related operations)
3 For all algorithms that take Compare, there is a version that uses operator< instead. That is, comp(*i, *j) != false defaults to *i < *j != false. For algorithms other than those described in 28.7.3, comp shall induce a strict weak ordering on the values.
4 The term strict refers to the requirement of an irreflexive relation (!comp(x, x) for all x), and the term weak to requirements that are not as strong as those for a total ordering, but stronger than those for a partial ordering...
struct setCompareFunctor { bool operator( )(const pair< int, int > &lhs, const pair< int, int > &rhs) const { return(lhs.second < rhs.second); } }; struct setCompareFunctorAux { bool operator( )(const pair< int, int > &lhs, const pair< int, int > &rhs) const { return(lhs.second < rhs.second); } bool operator( )(const pair< int, int > &lhs, int val) const { return(lhs.second < val); } bool operator( )(int val, const pair< int, int > &rhs) const { return(val < rhs.second); } };
Take into account that within the called algorithm there is used the operator
struct setCompareFunctorAux { //... bool operator( )(const pair< int, int > &lhs, int val) const { return(lhs.second < val); } };
Here is a demonstrative program
#include <iostream> #include <set> #include <algorithm> int main() { using namespace std; struct setCompareFunctor { bool operator( )(const pair< int, int > &lhs, const pair< int, int > &rhs) const { return(lhs.second < rhs.second); } }; struct setCompareFunctorAux { bool operator( )(const pair< int, int > &lhs, const pair< int, int > &rhs) const { return(lhs.second < rhs.second); } bool operator( )(const pair< int, int > &lhs, int val) const { return(lhs.second < val); } bool operator( )(int val, const pair< int, int > &rhs) const { return(val <= rhs.second); } }; set< pair< int, int >, setCompareFunctor > submultimi; submultimi.insert(make_pair(1, 15)); submultimi.insert(make_pair(2, 9)); submultimi.insert(make_pair(3, 33)); submultimi.insert(make_pair(4, 44)); submultimi.insert(make_pair(5, 20)); submultimi.insert(make_pair(6, 15)); for (const auto &p : submultimi) { cout << "{ " << p.first << ", " << p.second << " } "; } cout << endl; set< pair< int, int >, setCompareFunctor >::iterator it = lower_bound(submultimi.begin(), submultimi.end(), 20, setCompareFunctorAux()); cout << (*it).second << endl; return 0; }
Its output is
{ 2, 9 } { 1, 15 } { 5, 20 } { 3, 33 } { 4, 44 } 20
The expected result is 15, but the real result is 33.
And the expected and correct result is 20 because there is an element with the second value equal to 20 and you are searching exactly 20.
Take into account that the template class std::set has its own member function lower_bound.
setCompareFunctoris not a valid comparator as per the strict weak ordering criteria. The<=condition should be<. But that aside, how do you want your elements sorted? Why is the expected result 15?