I want to use upper_bound on array of pair and that too using a comparator function that compares the second parameter of pair. Please help.
Code:
bool cmp(const pair<int,int> &p1, const pair<int,int> &p2)
{
return p1.second<p2.second;
}
int subsets(pair<int,int> time[], int p)
{
if(p == 0) return 1;
int j = upper_bound(time, time+p, time[p], cmp) - time;
}
EDIT: I have corrected the return type but I don't seem to get what I wanted as an output.
I have an array of pair<int,int>
named time which contains the start and end time as the first and second parameter respectively and sorted according to end time in increasing fashion.
Presently I am at index p. I want to find the maximum index of the array( = j), such that time[j].second <= time[p].first
.
Eg.
time = { (1,5), (4,7) , (6, 12) } and if p = 2(0 based indexing) then j should be = 0 (as 5 <= 6 but 7 > 6) but upper_bound gives me j = 2.
How can I achieve this?
The following code does the job :)
bool compareit(int n, pair<int,int> &p)
{
return p.second > n;
}
int subsets(pair<int,int> time[], int p)
{
if(p == 0) return 1;
int j = upper_bound(time, time+p, time[p].first, compareit) - time;
}