Open In App

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

Last Updated : 13 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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;
}

Output
56
67

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;
}

Output
34
56

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;
}

Output
56 is found.

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;
}

Output
No. 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);



Next Article

Similar Reads