The size of the vector can be reduced by using different built-in functions of C++. The pop_back() function is one of them. It is used to remove the last element of the vector from the back and reduce the size of the vector by 1. But the last element of the vector is not removed permanently like the erase() function. The different uses of this function have been explained in this tutorial.
Syntax:
This function has not any argument, and it returns nothing.
Pre-requisite:
Before checking the examples of this tutorial, you have to check the g++ compiler is installed or not in the system. If you are using Visual Studio Code, then install the necessary extensions to compile the C++ source code to create the executable code. Here, the Visual Studio Code application has been used to compile and execute the C++ code. The ways to reduce the size of the vector using the pop_back() function have shown in the next part of this tutorial.
Example-1: Remove multiple elements from the vector
Create a C++ file with the following code to remove two elements from the vector container by reducing the size of the vector using the pop_back() function. A vector of 5 string values has been declared in the code. The pop_back() function has been called two times here to remove two last elements from the vector temporarily and reduce the size of the vector by 2. The content of the vector has been printed two times before and after using the pop_back() function.
#include <iostream>
#include <vector>
using namespace std;
int main() {
//Declare a vector of string values
vector<string> flowers = {"Rose", "Lity", "Marigold", "Tulip", "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 <iostream>
#include <vector>
using namespace std;
int main ()
{
//Declare a vector of integer data
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 <vector>
using namespace std;
int main()
{
//Declare a vector of integer data
vector<int> intVector{ 54, 19, 46, 72, 22, 83, 10, 53 };
//Declare an empty vector
vector<int> newVector;
//Declare an integer variable
int length;
//Print the last element based on the size of the vector
length = intVector.size();
cout << "The current size of the vector:" << length << "\n";
cout << "The last value of the vector before remove:" << intVector[length-1] << "\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.