In this article we will discuss the implementation of the lower_bound() and upper_bound() in an array of pairs.
- lower_bound(): It 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 Array 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 to the index which out of the array of pairs. - upper_bound(): It 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 Array 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 to the index which out of the array of pairs.
Syntax:
// For lower bound
lower_bound(array_name, array_name + array_size, value, comparator_function);// For upper bound
upper_bound(array_name, array_name + array_size, value, comparator_function);
Parameters: The function lower_bound() and upper_bound() in array of pairs accepts the following parameters:
- array_name & array_size: The name and size of the array which represents the interval between [start, end).
- value: Value of the lower_bound()/upper_bound() to be searched in the range.
- comparator_function: Binary function that accepts two arguments as its input, namely an element of type pair from the array, and the second is the value for which lower_bound()/upper_bound() has to be found and returns a boolean value.
Return Type: It returns an iterator pointing to the first element of the array whose first parameter is greater than or equal to the value.
Below is the program to demonstrate lower_bound() and upper_bound() in array of pairs:
C++
// C++ program to demonstrate lower_bound() // and upper_bound() in Array of Pairs #include <bits/stdc++.h> using namespace std; // Function to implement lower_bound() void findLowerBound(pair<int, int> arr[], pair<int, int>& p, int n) { // Given iterator points to the // lower_bound() of given pair auto low = lower_bound(arr, arr + n, p); cout << "lower_bound() for {2, 5}" << " is at index: " << low - arr << endl; } // Function to implement upper_bound() void findUpperBound(pair<int, int> arr[], pair<int, int>& p, int n) { // Given iterator points to the // lower_bound() of given pair auto up = upper_bound(arr, arr + n, p); cout << "upper_bound() for {2, 5}" << " is at index: " << up - arr << endl; } // Driver Code int main() { // Given sorted array of Pairs pair<int, int> arr[] = { { 1, 3 }, { 1, 7 }, { 2, 4 }, { 2, 5 }, { 3, 8 }, { 8, 6 } }; // Given pair {2, 5} pair<int, int> p = { 2, 5 }; // Size of array int n = sizeof(arr) / sizeof(arr[0]); // Function Call to find lower_bound // of pair p in arr findLowerBound(arr, p, n); // Function Call to find upper_bound // of pair p in arr findUpperBound(arr, p, n); return 0; } |
lower_bound() for {2, 5} is at index: 3
upper_bound() for {2, 5} is at index: 4
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Implementation of lower_bound() and upper_bound() in Vector of Pairs in C++
- Implementation of lower_bound and upper_bound on Set of Pairs in C++
- Implementation of lower_bound() and upper_bound() on Map of Pairs in C++
- Implementation of lower_bound() and upper_bound() in List of Pairs in C++
- Strassen’s Matrix Multiplication Algorithm | Implementation
- Least Frequently Used (LFU) Cache Implementation
- Prim's Algorithm (Simple Implementation for Adjacency Matrix Representation)
- Kruskal's Algorithm (Simple Implementation for Adjacency Matrix)
- ML - Neural Network Implementation in C++ From Scratch
- Implementation of file allocation methods using vectors
- Implementation of all Partition Allocation Methods in Memory Management
- Print all pairs in an unsorted array with equal sum
- Find k ordered pairs in array with minimum difference d
- What’s difference between “array” and “&array” for “int array[5]” ?
- Priority queue of pairs in C++ with ordering by first and second element
- Map of pairs in STL
- Binary search in sorted vector of pairs
- Find k pairs with smallest sums in two arrays | Set 2
- Priority queue of pairs in C++ (Ordered by first)
- Sets of pairs in C++
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.

