std::upper_bound and std::lower_bound for Vector in C++ STL
Last Updated :
02 Mar, 2024
Click here for Set 1 and Set 2 of Vectors.
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
#include <algorithm> // for lower_bound, upper_bound and sort
#include <iostream>
#include <vector> // for vector
using namespace std;
int main()
{
int gfg[] = { 5, 5, 5, 6, 6, 6, 7, 7 };
vector< int > v(gfg, gfg + 8);
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;
cout << *itr << endl;
auto it = lower_bound(arr, arr + n, 6)
- arr;
cout << it << endl;
auto itr2 = lower_bound(
arr, arr + n,
3);
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;
cout << *itr << endl;
auto it = upper_bound(arr, arr + n, 6)
- arr;
cout << it << endl;
auto itr2 = upper_bound(arr, arr + n,
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 |
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...