Pop last element of a vector in C++

In this article, we will discuss how we can pop the last element of a vector and reduce its size by one. Before getting into the topic, let’s discuss what are vectors and the different operations that can be performed on vectors.

Vectors are the container class in C++. It’s similar to arrays but it can be resizable, unlike arrays. So we generally called vectors as the dynamic arrays which allocate memory during run time. It stores homogeneous elements in continuous memory locations. We declare the vector as follows :

vector<data_type> obj_name;

We can perform many functions on vectors like front(), back(), at(), push_back() ,pop_back(), end() etc. Each function has its own purpose. Among all these functions we use push_back to insert the element at the end of the vector and pop_back to remove/pop the last element from the vector in C++.

Now I hope you get an idea regarding vectors so let’s get into the topic and discuss in depth how to pop the last element of a vector using pop_back() with an example program.

Pop last element of a vector using pop_back() function in C++ :

This function pop_back() belongs to the vector header file. So before using vectors in our code we have to include this std::vector header. The syntax to use this function is

vect_obj.pop_back();

here vect_obj is the name of the vector we have created. We do not pass any parameters in this function and it does not have a return value.

void pop_back();

This function will remove the last element from the vector. Other than this we can use erase() function along with the last element index as a parameter to remove the last element of a vector. But we mostly use pop_back() to pop the last element of a vector. In general, this function will not throw exceptions but if we use an empty vector to call this function it shows undefined behavior. Let’s see a program to get a clear understanding.

#include <iostream>
#include <vector>
using namespace std;
int main() {
    
    vector<string> v;
    v.push_back("Hello");
    v.push_back("Coders");
    v.push_back("Hi");
    for(int i = 0 ;i < v.size(); i++)
    {
        cout<<"element at index "<<i<<" is : "<<v[i]<<"\n";
    }
    v.pop_back();
    cout<<"After removing last element\n";
    for(int i = 0 ;i < v.size(); i++)
    {
        cout<<"element at index "<<i<<" is : "<<v[i]<<"\n";
    }
    return 0;
}

Output :

element at index 0 is : Hello
element at index 1 is : Coders
element at index 2 is : Hi
After removing last element
element at index 0 is : Hello
element at index 1 is : Coders

In the above code initially, we include the “vector” header file and created a vector v that stores string elements. later we inserted elements at the end of the vector three times using the push_back() function. Next, we pop the last element so the “hi” string got removed. We observed that the size of the vector was also reduced by one. Hope you have understood the entire discussion.

Leave a Reply

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