Implementation of lower_bound and upper_bound on Set of Pairs in C++

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 and second value is greater than equals to y.
If the above-mentioned criteria are not met, then it returns an iterator which points to the last pair of the set container.

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 and second value is greater than y.
If the above-mentioned criteria are not met, then it returns an iterator which points to the last pair of the set container.

Syntax: There are two ways to use upper_bound():

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to demonstrate lower_bound()
// and upper_bound() in set of Pairs
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to implement lower_bound()
void findLowerBound(
    set<pair<int, int> >& arr,
    pair<int, int>& p)
{
  
    // Given iterator points to the
    // lower_bound() of given pair
    auto low = lower_bound(arr.begin(),
                           arr.end(), p);
  
    cout << "lower_bound() for {1, 2}"
         << " is: {" << (*low).first << ", "
         << (*low).second << "}" << endl;
}
  
// Function to implement upper_bound()
void findUpperBound(
    set<pair<int, int> >& arr,
    pair<int, int>& p)
{
  
    // Given iterator points to the
    // lower_bound() of given pair
    auto up = upper_bound(arr.begin(),
                          arr.end(), p);
  
    cout << "upper_bound() for {1, 2}"
         << " is: {" << (*up).first << ", "
         << (*up).second << "}" << endl;
}
  
// Driver Code
int main()
{
  
    // Given sorted vector of Pairs
    set<pair<int, int> > s;
  
    // Insert pairs in set
    s.insert(make_pair(1, 2));
    s.insert(make_pair(2, 4));
    s.insert(make_pair(3, 7));
    s.insert(make_pair(8, 9));
  
    // Given pair { 1, 2 }
    pair<int, int> p = { 1, 2 };
  
    // Function Call to find lower_bound
    // of pair p in arr
    findLowerBound(s, p);
  
    // Function Call to find upper_bound
    // of pair p in arr
    findUpperBound(s, p);
  
    return 0;
}

chevron_right


Output:

lower_bound() for {1, 2} is: {1, 2}
upper_bound() for {1, 2} is: {2, 4}

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.