CPP Program Using Vector STL Lower_bound Method

Beginner

Introduction

In this lab, we will learn how to use the lower_bound() method in the Vector STL in C++. The practical implementation of this method will be shown in the lab.

Creating a Vector and Filling it with Integers

What are Vectors?

Vectors are dynamic arrays with the added ability to resize themselves and automatically adjust their size when we insert or delete elements. Vectors provide more flexibility compared to normal static arrays.

In this step, we will create an empty vector and insert some elements into it. Note that since vectors are dynamic, we don't need to specify the size of the vector before inserting elements.

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

using namespace std;

int main()
{
    // create an empty vector
    vector<int> v;

    // insert elements into the vector
    v.push_back(10);
    v.push_back(12);
    v.push_back(35);
    v.push_back(65);
    v.push_back(21);
    v.push_back(90);
}

Sorting the Elements of the Vector

In this step, we will sort the elements of the vector in ascending order using the sort() function from the <algorithm> library.

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

Using the lower_bound() Method in the Vector STL

Now, we will use the lower_bound() method to find an iterator pointing to the first element which has a value not less than the given value.

// define the iterator low
vector<int>::iterator low;

// use lower_bound to find the first element that's not less than 35
low = lower_bound(v.begin(), v.end(), 35);

Printing the Results

In this step, we will print the index of the lower bound of 35 and some notes on the result.

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

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

Complete code

Here is the complete code for the main.cpp file:

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

using namespace std;

int main()
{
    // create an empty vector
    vector<int> v;

    // insert elements into the vector
    v.push_back(10);
    v.push_back(12);
    v.push_back(35);
    v.push_back(65);
    v.push_back(21);
    v.push_back(90);

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

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

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

    // print the sorted vector
    cout << "\n\nThe elements of the Vector after Sorting are: ";
    for (int i : v)
    {
        cout << i << " ";
    }

    // define the iterator low
    vector<int>::iterator low;

    // use lower_bound to find the first element that's not less than 35
    low = lower_bound(v.begin(), v.end(), 35);

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

    // print some notes
    cout << "\nNote that as per the definition, it also considers the number itself.\n\n";
    return 0;
}

Running the program

To run the program, open a terminal window and navigate to the directory containing the main.cpp file. Use the following commands to compile and run the program:

g++ main.cpp -o main && ./main

This should compile and run the program, and you should see the following output:

The elements of the Vector are: 10 12 35 65 21 90

The elements of the Vector after Sorting are: 10 12 21 35 65 90

The index (starting from 0) of the lower_bound of 35 is: 3

Note that as per the definition, it also considers the number itself.

Summary

Congrats! In this lab, you learned about the lower_bound() method in the Vector STL in C++, and you saw its implementation using vectors. With these skills, you can create, sort, and search for elements in vectors with precision. Great job!

Other Tutorials you may like