lower_bound() in C++ STL

The C++ Standard Library consists of the header, <algorithm> which defines a collection of functions (principally designed to deal with a range of elements). lower_bound() is a method in the STL that returns an iterator pointing to the first element in the range which does not compare less than a certain value. The method returns the last iterator if all the elements in the range compare less than the value.


lower_bound( iterator_begin, iterator_end, val );

Parameters: The lower_bound() method accepts 3 parameters

  • iterator_begin: Iterator to the initial position
  • iterator_end: Iterator to the final position ( Note: the range is [iterator_begin,iterator_end) )
  • val: Value of the lower bound to search for in the range

Return value: An iterator to the lower bound of val in the range. iterator_end if all elements compare less than val

Example of lower_bound() method

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int main()
    vector<int> v;

    vector<int>::iterator it = lower_bound(v.begin(), v.end(), 5);
    cout<<"Lower bound index of element 5 is: "<< distance(v.begin(), it) << endl;


Lower bound index of element 5 is: 2

This article is written by

Please comment below, if you have any doubts or find any error in the above article.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top
[gravityforms id="5" description="false" titla="false" ajax="true"]