Gice

Technology and General Blog

The sizing of the vector can be minimized by employing diverse developed-in capabilities of C++. The pop_back() perform is a single of them.  It is utilized to take away the previous ingredient of the vector from the back and minimize the dimensions of the vector by 1. But the final factor of the vector is not eliminated permanently like the erase() purpose. The unique utilizes of this operate have been explained in this tutorial.

Syntax:

This perform has not any argument, and it returns very little.

Pre-requisite:

Before checking the examples of this tutorial, you have to examine the g++ compiler is put in or not in the technique. If you are utilizing Visual Studio Code, then install the needed extensions to compile the C++ supply code to create the executable code. Right here, the Visual Studio Code software has been utilized to compile and execute the C++ code. The techniques to decrease the measurement of the vector working with the pop_back again() operate have demonstrated in the next section of this tutorial.

Instance-1: Take out a number of elements from the vector

Build a C++ file with the following code to remove two factors from the vector container by lowering the size of the vector applying the pop_again() operate. A vector of 5 string values has been declared in the code. The pop_back() purpose has been referred to as two moments below to take away two past aspects from the vector quickly and lessen the dimensions of the vector by 2. The articles of the vector has been printed two instances in advance of and just after utilizing the pop_back() purpose.

//Incorporate necessary libraries

#involve

#involve

using namespace std

int key()

        //Declare a vector of string values

        vector<string> flowers = “Rose”, “Lity”, “Marigold”, “Tulip”, “Drinking water Liiy”

        cout << “The values of the vector :n

        //Iterate the vector using loop to print the values

        for(int i = 0 i < flowers.size() ++i)

            cout << flowers[i] << ”  “

        cout << n

        //Remove the last two values from the vector

        flowers.pop_back()

        flowers.pop_back()

        cout << nThe values of the vector after remove :n

        //Iterate the vector using loop to print the values

        for(int i = 0 i < flowers.size() ++i)

            cout << flowers[i] << ”  “

        cout << n

        return 0

Output:

The following output will appear after executing the above code.

Example-2:  Create a new vector from another vector

Create a C++ file with the following code to insert specific values into an empty vector from another vector by removing the elements using the pop_back() function.  A vector of 8 integer numbers and an empty vector of integer type have been declared in the code. The ‘while’ loop has been used to iterate each element of the first vector and insert the element into the new vector if the number is divisible by 2. The sum of all even numbers has also been calculated here. Each element of the first vector will be removed by the pop_back() function in each iteration of the loop to reach the loop’s termination condition.

//Include necessary libraries

#include

#consist of

making use of namespace std

int key ()

        //Declare a vector of integer knowledge

        vector<int> intVector 5, 9, 4, 7, 2, 8, 1, 3

        //Declare an empty vector

        vector<int> newVector

        cout << “The values of the original vector :n

        //Iterate the vector using loop to print the values

        for(int i = 0 i < intVector.size() ++i)

                cout << intVector[i] << ” “

        cout << n

        //Initialize the result

        int result = 0

        //Iterate the loop until the vector becomes empty

        while(!intVector.empty())

       

                /*

                Find out the even numbers to insert into the newVector

                and calculate the sum of the even numbers

                */

                if (intVector.back() % 2 == 0)

               

                        result += intVector.back()

                        newVector.push_back(intVector.back())

               

                //Remove element from the end of the intVactor

                intVector.pop_back()

       

        cout << “The values of the new vector :n

        //Iterate the vector using loop to print the values

        for(int i = 0 i < newVector.size() ++i)

                cout << newVector[i] << ” “

        cout << n

        cout << “The sum of all even numbers : “ << result << n

        return 0

Output:

The following output will appear after executing the above code.  There were three even numbers in the first vector. There are 8, 2, and 4.

Example-3: Check the last element of the vector is removed or not

It is mentioned earlier that the pop_back() does not remove the elements permanently from the vector, and it removes the element by reducing the size of the vector only. So, the removed element remains in the same position until the vector’s size increases and replaces the element with another element. Create a C++ file with the following code to check the element removed by the pop_back() function exists or not. The last position of the original vector has been printed before and after using the pop_back() function.

#include

#contain

using namespace std

int most important()

        //Declare a vector of integer facts

        vector<int> intVector 54, 19, 46, 72, 22, 83, 10, 53

        //Declare an vacant vector

        vector<int> newVector

        //Declare an integer variable

        int size

        //Print the very last component based on the size of the vector

        duration = intVector.sizing()

        cout << “The current size of the vector:” << length << n

        cout << “The last value of the vector before remove:” << intVector[length1] << n

        //Remove the element from the end of the vector

        intVector.pop_back()

        //Print the last element based the size of the vector after remove

        length = intVector.size()

        cout << “The current size of the vector:” << length << n

        cout << “The last value of the vector after remove:” << intVector[length] << n

        return 0

Output:

The following output will appear after executing the above code.  The output shows that the size of the vector is reduced by 1, but the element of the last position of the original vector still exists.

Conclusion:

Three different uses of the pop_back() function have been described in this tutorial using simple examples. The main purpose of using this function will be cleared for the readers after practicing the examples of this tutorial.

Leave a Reply

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