Signup/Sign In

C++ Program using lower_bound() method in Vector STL

Hello Everyone!

In this tutorial, we will learn the working of the lower_bound() method in STL and its implementation using Vector, in the C++ programming language.

What are Vectors?

Vectors are the same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted. This makes them more advantageous over the ordinary Arrays which are of fixed size and are static in nature.

To learn more about Vectors in CPP, we will recommend you to visit C++ STL Vector

The lower_bound() method:

The lower_bound() method returns an iterator pointing to the first element which has a value not less than the given value.

For better understanding, refer to the well-commented C++ code given below.


#include <iostream>
#include <bits/stdc++.h>

using namespace std;

int main()
    cout << "\n\nWelcome to Studytonight :-)\n\n\n";
    cout << " =====  Program to demonstrate the working of lower_bound() method of STL, in CPP  ===== \n\n";

    cout << "\n\nDeclaring a Vector and Filling it with integers.\n\n";

    //create an empty vector
    vector<int> v;

    //insert elements into the vector

    //prining the vector
    cout << "The elements of the Vector are: ";

    //Another way of printing the elements using the for loop
    for (int i : v)
        cout << i << " ";

    //Sorting the vector in ascending order
    sort(v.begin(), v.end());

    //prining the Sorted vector
    cout << "\n\nThe elements of the Vector after Sorting are: ";

    for (int i : v)
        cout << i << " ";

    vector<int>::iterator low;

    low = lower_bound(v.begin(), v.end(), 35);

    cout << "\n\nlower_bound returns an iterator pointing to the first element which has a value not less than the given value.";

    cout << "\n\nThe index (starting from 0) of the lower_bound of 35 is: " << (low - v.begin()) << '\n';

    cout << "\n\nNote that as per the definition, it also considers the number itself.\n";

    cout << "\n\n\n";

    return 0;


C++ lower bound

We hope that this post helped you develop better understanding of the concept of lower_bound() method and its implementation in CPP. For any query, feel free to reach out to us via the comments section down below.

Keep Learning : )

About the author:
Nikita Pandey is a talented author and expert in programming languages such as C, C++, and Java. Her writing is informative, engaging, and offers practical insights and tips for programmers at all levels.