Open In App

std::upper_bound and std::lower_bound for Vector in C++ STL

Improve
Improve
Like Article
Like
Save
Share
Report

Click here for Set 1 and Set 2 of Vectors.   

Vector – upper_bound and lower_bound

Iterator lower_bound (Iterator first, Iterator last, const val)  
lower_bound returns an iterator pointing to the first element in the range [first,last) which has a value not less than ‘val’  and if the value is not present in the vector then it returns the end iterator.

Iterator upper_bound (Iterator first, Iterator last, const val)
upper_bound returns an iterator pointing to the first element in the range [first,last) which has a value greater than ‘val’  and if the value is not present in the vector then it returns the end iterator.

CPP




// lower_bound and upper_bound in vector
 
#include <algorithm> // for lower_bound, upper_bound and sort
#include <iostream>
#include <vector> // for vector
 
using namespace std;
 
int main()
{
    // Note that the array is sorted
    int gfg[] = { 5, 5, 5, 6, 6, 6, 7, 7 };
 
    vector<int> v(gfg, gfg + 8); // 5 5 5 6 6 6 7 7
 
    vector<int>::iterator lower, upper;
    lower = lower_bound(v.begin(), v.end(), 6);
    upper = upper_bound(v.begin(), v.end(), 6);
 
    cout << "lower_bound for 6 at index "
         << (lower - v.begin()) << '\n';
    cout << "upper_bound for 6 at index "
         << (upper - v.begin()) << '\n';
 
    return 0;
}


Output

lower_bound for 6 at index 3
upper_bound for 6 at index 6

Time Complexity: O(log(n)) where n is the number of elements in the array.
Auxiliary Space: O(1)

Different functionalities on Lower Bound Code in C++:

C++




#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    int n;
    cin >> n;
    int arr[n];
 
    for (int x = 0; x < n; x++) {
        cin >> arr[x];
    }
   
    sort(arr, arr+n);
   
    auto itr = lower_bound(arr, arr + n, 6);
    cout << itr
         << endl; // returns the address position at which 6
                  // is present in memory if it is present
    cout << *itr << endl; // returns the no. stored at the
                          // itr memory address
    auto it = lower_bound(arr, arr + n, 6)
              - arr; // returns the index position of
                     // searched element in array
    cout << it << endl;
    auto itr2 = lower_bound(
        arr, arr + n,
        3); // if the searched element is not present then
            // it will give next greater element
    cout << *itr2;
    return 0;
}


Output:

5
1 2 4 5 6
0x46f25ff7b0
6
4
4

Different functionalities on Upper Bound Code in C++:

C++




#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    int n;
    cin >> n;
    int arr[n];
 
    for (int x = 0; x < n; x++) {
        cin >> arr[x];
    }
   
    sort(arr, arr+n)
   
    auto itr = upper_bound(arr, arr + n, 6);
    cout << itr << endl; // returns the address position
                         // which has value greater than 6
    cout << *itr << endl; // returns the no. stored at the
                          // itr memory address
    auto it = upper_bound(arr, arr + n, 6)
              - arr; // returns the index position of itr
                     // element in array
    cout << it << endl;
    auto itr2 = upper_bound(arr, arr + n,
                            3); // gives the element which
                                // has value greater than 3
    cout << *itr2;
    return 0;
}


Output:

5
1 6 8 10 14
0x40ebdffc28
8
2
6

Let us see the difference table with 5 useful differences that are as follows: 

std::upper_bound

std::lower_bound

It is used to return an iterator pointing to the last element in the range It is used to return  an iterator pointing to the first element in the range
It is defined in <algorithm> header file. It is defined in <algorithm> header file.
Its return type is the iterator of the given type. Its return type is the iterator of the given type.
Its complexity is logarithmic. Its complexity is logarithmic.
If no element in the range compares greater than val, the function returns last. If all the element in the range compare less than val, the function returns last

 

 



Last Updated : 02 Mar, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads