vector::pop_back() function in C++

In C++, the vector class provides a function pop_back(). It removes the last element from the calling vector object.

Table Of Contents

Syntax of vector::pop_back():

void pop_back();

Parameters:

None

Return value:

Advertisements

None

It accepts no argument, and does not return anything. It just removes an element from the end of the vector. Also it will decrement the size of the vector by one.

Example of vector::pop_back() function

Let’s see an example, where we have a vector that contains seven values. Now we want to delete the last value from this vector. For that we will call the pop_back() function on the vector object. Let’s see the example,

#include <iostream>
#include <vector>

int main ()
{
    // create a vector, and initialize with seven integers
    std::vector<int> vectorObj {11, 12, 13, 14, 15, 16, 17};

    // Removes an element from 
    // the end of vector
    vectorObj.pop_back();

    // print all elements in vector
    for(const int& num: vectorObj){
        std::cout<< num << ", ";
    }
    std::cout<< std::endl;

    return 0;
}

Output:

11, 12, 13, 14, 15, 16,

Here the pop_back() function deleted the last element from the vector.

Calling vector::pop_back() function on empty vector

There is an important point that we need to keep in mind while using the pop_back() function. If vector is empty, then the pop_back() function can result in undefined behaviour. So we always need to check if vector is empty or not before calling the pop_back() function.

Let’s see and another example, where we have a vector of string which contains 5 string objects and we will delete the last element from this vector by calling the pop_back() function. But before that we will check if vector is not empty, then only remove the last element from the vector using pop_back().

#include <iostream>
#include <vector>
#include <string>

int main ()
{
    // create a vector, and initialize with five strings
    std::vector<std::string> vectorObj {"Hi", "this", "is", "a", "sample"};

    // Check if vector is not empty
    if(!vectorObj.empty())
    {
        // Remove last element from the vector
        vectorObj.pop_back();
    }

    // print all elements in vector
    for(const std::string& elem: vectorObj){
        std::cout<< elem << " ";
    }
    std::cout<< std::endl;

    return 0;
}

Output:

Hi this is a 

It deleted the last element from vector.

Remove all elements from vector using vector::pop_back()

We can also use the pop_back() function to remove all elements from the vector. For that we need to call the pop_back() function inside your while loop. This while loop will run until vector is not empty. Let’s see the example,

#include <iostream>
#include <vector>

int main ()
{
    // create a vector, and initialize with seven integers
    std::vector<int> vectorObj {11, 12, 13, 14, 15, 16, 17};

    // Iterate till vector is not empty
    while(!vectorObj.empty())
    {
        // Removes an element from 
        // the end of vector
        vectorObj.pop_back();
    }

    // print all elements in vector
    for(const int& num: vectorObj){
        std::cout<< num << ", ";
    }
    std::cout<< std::endl;

    return 0;
}

It will delete all elements from the vector using a while loop and pop_back() function.

Summary

We learned how to use the pop_back() function of vector in C++. Thanks.

Advertisements

Thanks for reading.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top