VOOZH about

URL: https://www.geeksforgeeks.org/cpp/set-lower_bound-function-in-c-stl/

⇱ set::lower_bound() Function in C++ STL - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

set::lower_bound() Function in C++ STL

Last Updated : 11 Jul, 2025

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++.

Example:


Output
5

set::lower_bound() Syntax

s.lower_bound(val);

Parameters

  • val: Value whose lower bound is to be searched.

Return Value

  • Returns an iterator to the first element that is equal to or just greater than the given value.
  • If all the elements are less than to the given value, returns an iterator to the end of the std::set.

More Examples of std::lower_bound()

The following examples demonstrate the behaviors of set::lower_bound() function in different cases.

Example 1: Finding Lower Bound in Set of Strings


Output
geeks

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

Example 2: Trying to Find Lower Bound that is Not Present in Set


Output
Lower Bound NOT Present.

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

Example 3: Searching for Element in Set using set::lower_bound()


Output
7 is present.

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

Working of set::lower_bound()

The std::set::lower_bound() function uses the search operation of std::set container which is internally implemented as self-balancing binary search tree. So, for searching the lower bound of any value, we move from the root node of the tree using the path that may contain the lower bound.

  • If we find the lower bound in this path, we quit the search and return the iterator to it.
  • If we reach the leaf node without finding it, we return iterator to the end.

That is why, the complexity of the set::lower_bound() function is same as the search operation of self-balancing binary search tree.

Comment