Prerequisite: set lower_bound() function in C++ STL , set upper_bound() function in C++ STL
lower_bound() returns an iterator pointing to the first element in the range [first, last) which has a value greater than or equals to the given value “val” . But in set of Pairs lower_bound() for pair(x, y) will return an iterator pointing to the position of pair whose the first value is greater than or equals x .
If first values are same, then it will return an iterator pointing to the position of pair whose the second value is greater than or equals y .
If the above-mentioned criteria are not met, then it returns an iterator which points to end of the set (next to the last pair in set).
Syntax: There are two ways to use lower_bound():
setName.lower_bound({a, b})
lower_bound(setName.begin(), setName.end(), pair(a, b))
upper_bound() returns an iterator pointing to the first element in the range [first, last) which has a value greater than the given value “val” . But in set of Pairs upper_bound() for pair(x, y) will return an iterator pointing to the position of pair whose the first value is greater than x .
If first values are same, then it will return an iterator pointing to the position of pair whose the second value is greater than y .
If the above-mentioned criteria are not met, then it returns an iterator which points to end of the set (next to the last pair in set).
Syntax: There are two ways to use upper_bound() with Set:
setName.upper_bound({a, b})
upper_bound(setName.begin(), setName.end(), pair(a, b))
Below is the program to demonstrate lower_bound() and upper_bound() in set of pairs :
#include <bits/stdc++.h>
using namespace std;
void findLowerBound(
set<pair<int, int> >& arr,
pair<int, int>& p)
{
auto low = lower_bound(arr.begin(),
arr.end(), p);
cout << "lower_bound() for {1, 2}"
<< " is: {" << (*low).first << ", "
<< (*low).second << "}" << endl;
}
void findUpperBound(
set<pair<int, int> >& arr,
pair<int, int>& p)
{
auto up = upper_bound(arr.begin(),
arr.end(), p);
cout << "upper_bound() for {1, 2}"
<< " is: {" << (*up).first << ", "
<< (*up).second << "}" << endl;
}
int main()
{
set<pair<int, int> > s;
s.insert(make_pair(1, 2));
s.insert(make_pair(2, 4));
s.insert(make_pair(3, 7));
s.insert(make_pair(8, 9));
pair<int, int> p = { 1, 2 };
findLowerBound(s, p);
findUpperBound(s, p);
return 0;
}
Output:
lower_bound() for {1, 2} is: {1, 2}
upper_bound() for {1, 2} is: {2, 4}
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now !
Looking for a place to share your ideas, learn, and connect? Our Community portal is just the spot! Come join us and see what all the buzz is about!
Last Updated :
22 Jul, 2020
Like Article
Save Article
Share your thoughts in the comments
Add Your Comment
Please Login to comment...