YetiCodeCamp - C++ for Beginners

Companion website for the C++ for Beginners YouTube video series.

C++ Vector pop_back() Function


The C++ vector.pop_back() function deletes the element at the end of a vector and reduces the size of the vector by one. This function does not take a parameter.

Syntax:

Example, using a vector “v1”.

v1.pop_back();

Deletes the last element of v1 and reduces its size by one.





This example is a complete program using the vector pop_back() function to remove the last element in a vector.

#include<iostream>
#include<vector>

int main()
{
std::vector<int> numbers{1,2,3,4,5,6,7,8,9,10};

std::cout << numbers.size() << "\n";

numbers.pop_back();

for( v : numbers)
    std::cout << v << " ";

std::cout << "\n" << numbers.size();

return 0;
}

Output:
10
1 2 3 4 5 6 7 8 9
9