std::upper_bound and std::lower_bound for Vector in C++ STL
Last Updated :
13 Feb, 2025
The std::upper_bound() and std::lower_bound() functions are used for binary search operations STL containers that provide random access. They both are defined inside <algorithm> header file. In this article, we will learn how to use the std::upper_bound and std::lower_bound for vector in C++ STL.
lower_bound() for Vectors
The std::lower_bound() method can be used to find the first value that is greater than or equal to given value in the vector. It needs the vector to be sorted because it implements binary search to find the value.
Syntax
std::lower_bound(first, last, val, comp);
upper_bound() for Vectors
The std::upper_bound() method can be used to find the first value that is greater than to given value in the vector. It needs sorted vector because it implements binary search to find the value.
Syntax
std::upper_bound(first, last, val, comp);
Examples
The below examples demonstrate the use of std::upper_bound and std::lower_bound in a vector in different scenarios.
Example 1: Finding Lower and Upper Bound in a Sorted Vector
C++
// C++ Program to illustrate use of std::lower_bound()
// and std::upper_bound() for vector
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v = {11, 34, 56, 67, 89};
// Finding lower bound of 56
cout << *lower_bound(v.begin(), v.end(), 56)
<< endl;
// Finding upper bound of 56
cout << *upper_bound(v.begin(), v.end(), 56);
return 0;
}
Time Complexity: O(log n), where n is the number of elements in vector.
Auxiliary Space: O(1)
Example 2: Finding the Lower and Upper Bound in a Part of Unsorted Vector
C++
// C++ program to find the upper and lower bound in
// the part of a vector
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v = {89, 11, 56, 34, 67};
// Sorting v before finding upper and lower bound
sort(v.begin(), v.end());
// Finding the lower bound of value 30 in the
// first three elements of the vector v
cout << *lower_bound(v.begin(), v.begin() + 3, 30)
<< endl;
// Finding the upper bound of value 30 in the
// last three elements of the vector v
cout << *upper_bound(v.end() - 3, v.end(), 30);
return 0;
}
Time Complexity: O(log n), not considering time required for sorting.
Auxiliary Space: O(1), not considering memory required for sorting.
Explanation: As we know that std::upper_bound() and std::lower_bound() doesn’t works for unsorted vector, so we use std::sort() function to sort the vector. After that, we find the lower and upper bound in the desired range.
Example 3: Checking if an Element Exists in Vector using lower_bound()
C++
// C++ Program to check if the element exists
// in a vector using lower_bound()
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v = {11, 34, 56, 67, 89};
int val = 56;
// Cheking if val exists in the vector v
auto it = lower_bound(v.begin(), v.end(), val);
if (*it == val)
cout << val << " is found.";
else
cout << val << " is NOT found.";
return 0;
}
Time Complexity: O(log n), where n is the number of elements in vector.
Auxiliary Space: O(1)
Explanation: The lower_bound() function will return the iterator to the given value if it is present in the vector. If it is not present, it will return iterator to the largest element smaller than the given value. We cannot use the upper_bound() function to find the given value in the same way.
Example 4: Finding the Number of Elements Smaller and Greater than a Value
C++
// C++ Program to count the elements smaller and larger than
// a value in a vector using lower_bound() and upper_bound()
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v = {11, 34, 56, 67, 89};
int val = 50;
// Finding lower and upper boud of val in v
auto lb = lower_bound(v.begin(), v.end(), val);
auto up = upper_bound(v.begin(), v.end(), val);
// Finding the number of smaller elements
cout << "No. of Smaller Elements: " << lb - v.begin()
<< endl;
// Finding the number of larger elements
cout << "No. of Larger Elements: " << v.end() - up
<< endl;
return 0;
}
OutputNo. of Smaller Elements: 2
No. of Larger Elements: 3
Time Complexity: O(log n), where n is the number of elements in vector.
Auxiliary Space: O(1)
Explanation: The lower_bound() function will return the iterator to the first element just greater than or equal to the given value in the sorted vector. It means that all the elements previous to this will be less than the give value, so we can substract the vector.begin() iterator from the iterator returned by the lower_bound() to get the number of elements.
Difference Between lower_bound() and upper_bound()
Following are the main differences between the std::lower_bound() and std::upper_bound():
std::lower_bound
| std::upper_bound
|
---|
Finds the first position where the value is not less than the given value (≤). | Finds the first position where the value is greater than the given value (>). |
Returns an iterator pointing to the first element that is greater than or equal to the specified value. | Returns an iterator pointing to the first element that is greater than the specified value. |
Syntax: lower_bound(first, last, val, comp);
| Syntax: upper_bound(first, last, val, comp);
|
Similar Reads
Implementation of lower_bound() and upper_bound() in Vector of Pairs in C++
Here we will discuss the implementation of the lower_bound() and upper_bound() in vector 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 equal to the given value "val". But in Vector of Pairs lower_bound() for
3 min read
Difference between std::set::upper_bound and std::upper_bound in C++
Prerequisites: Random-access Iterators, Bidirectional Iterators Sets are a type of associative container in which each element has to be unique because the value of the element identifies it. The value of the element cannot be modified once it is added to the set, though it is possible to remove and
4 min read
Binary Search functions in C++ STL (binary_search, lower_bound and upper_bound)
In C++, STL provide various functions like std::binary_search(), std::lower_bound(), and std::upper_bound() which uses the the binary search algorithm for different purposes. These function will only work on the sorted data. There are the 3 binary search function in C++ STL: Table of Content binary_
3 min read
Difference between std::set::lower_bound and std::lower_bound in C++
Prerequisite: Random-access Iterators in C++, Bidirectional Iterators in C++. std::lower_bound in C++: The lower_bound() method in C++ is used to return an iterator pointing to the first element in the range [first, last) which has a value not less than the given value. This means that the function
4 min read
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) wi
3 min read
Implementation of lower_bound() and upper_bound() in List of Pairs in C++
In this article, we will discuss the implementation of the lower_bound() and upper_bound() in a list 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 List of Pairs lower_
3 min read
vector::push_back() and vector::pop_back() in C++ STL
In C++, the vector push_back() is a built-in method used to add a new element at the end of the vector. It automatically resizes the vector if there is not enough space to accommodate the new element. Letâs take a look at an example that illustrates the vector push_back() method: [GFGTABS] C++ #incl
2 min read
set::upper_bound() function in C++ STL
In C++, the set upper_bound() is a built-in method used to find the first element in the set that is just greater than the given value. In this article, we will learn about set upper_bound() function in C++. Letâs take a quick look at a simple illustration of the function: [GFGTABS] C++ #include
3 min read
set::lower_bound() function in C++ STL
The std::set::lower_bound() method is used to find the first element in the set that is equal to or greater than the given value. It is a member function of std::set class and is defined inside <set> header file. In this article, we will learn about std::set::lower_bound() function in C++. Exa
3 min read
vector::front() and vector::back() in C++ STL
In C++, the vector front() is a built-in function used to retrieve the first element of the vector. It provides a reference to the first element, allowing you to read or modify it directly. Letâs take a quick look at a simple example that uses the vector front() method: [GFGTABS] C++ #include <bi
2 min read