Dhruv Mullick
Dhruv Mullick

Reputation: 561

lower_bound on a vector of pair<int,int> works, upper_bound does not

I have a sorted vector of pairs. To find the lower_bound and upper_bound, I have used:

bool cmp2(const pair<int,int> &p1, const int v)
{
    if(p1.first<v)
        return true;
    else
        return false;
}
auto it1 = lower_bound(V.begin(), V.end(), h, cmp2);
auto it2 = upper_bound(V.begin(), V.end(), h, cmp2);

Here h is an integer. What intrigues me is that a problem is there only in upper_bound. If I comment out upper_bound, my program builds successfully. What could be the reason for this?

Upvotes: 1

Views: 3028

Answers (1)

Steve Jessop
Steve Jessop

Reputation: 279245

upper_bound calls the comparator with the arguments in the other order (search value first, then element from the range it's searching).

So your comparator doesn't work, since it requires a pair as its first argument and an int as its second.

Upvotes: 3

Related Questions