In this C++ tutorial, you will learn how to remove the last element from a vector using vector::pop_back() function, with example program.

C++ Vector pop_back() Removes the Last Element

The std::vector::pop_back() function removes the element at the end of a C++ vector. After the call, the vector contains one fewer element.

For example, if a vector contains {24, 81, 57}, calling pop_back() removes 57 and leaves {24, 81}.

Syntax of C++ vector::pop_back()

</>
Copy
vector_name.pop_back();

pop_back() does not take any arguments and does not return the removed value. Its return type is void.

What Changes After vector::pop_back()

  • The last element is removed.
  • The vector’s size() decreases by one.
  • The vector’s capacity() is not reduced by pop_back().
  • The removed element is destroyed.
  • References and iterators to the erased element are invalidated, and the previous end() iterator is no longer valid.

The operation has constant time complexity because it removes only the final element; it does not shift the remaining elements.

C++ Vector pop_back() Examples

1. Remove the Last Integer from a Vector

In the following C++ program, we define a vector of integers, and added some elements to it. Then we shall call pop_back() function on the vector and remove the last element.

C++ Program

</>
Copy
#include <iostream>
#include <vector>
using namespace std;

int main() {
   vector<int> nums;
   nums.push_back(24);
   nums.push_back(81);
   nums.push_back(57);

   nums.pop_back();

   for (int element: nums)
      cout << element << endl;
}

Output

24
81

The last element, 57, is removed. The remaining elements are 24 and 81.

2. Check That the Vector Is Not Empty Before pop_back()

Do not call pop_back() on an empty vector. Check empty() first when the vector may contain no elements.

</>
Copy
#include <iostream>
#include <vector>
using namespace std;

int main() {
    vector<int> nums;

    if (!nums.empty()) {
        nums.pop_back();
    } else {
        cout << "Vector is empty";
    }

    return 0;
}

Output

Vector is empty

Calling pop_back() when the vector is empty is invalid and can result in undefined behavior. The empty() check prevents that call.

3. Get the Last Vector Element Before Removing It

vector::pop_back() removes the last element but does not return it. If you need the value, read it with back() first and then call pop_back().

</>
Copy
#include <iostream>
#include <vector>
using namespace std;

int main() {
    vector<int> nums = {10, 20, 30};

    if (!nums.empty()) {
        int last = nums.back();
        nums.pop_back();

        cout << "Removed: " << last << '\n';
        cout << "New size: " << nums.size();
    }

    return 0;
}

Output

Removed: 30
New size: 2

4. Remove Several Elements from the End of a Vector

You can call pop_back() repeatedly to remove multiple elements from the end. Each call removes exactly one element.

</>
Copy
#include <iostream>
#include <vector>
using namespace std;

int main() {
    vector<int> nums = {10, 20, 30, 40, 50};

    nums.pop_back();
    nums.pop_back();

    for (int num : nums) {
        cout << num << " ";
    }

    return 0;
}

Output

10 20 30

vector::pop_back() Does Not Return the Removed Value

A common mistake is to try to assign the result of pop_back() to a variable. This does not work because pop_back() returns void.

Use this two-step pattern when you need the last value:

</>
Copy
auto value = numbers.back();
numbers.pop_back();

Make sure the vector is not empty before calling either back() or pop_back().

vector::pop_back() Compared with erase()

Use pop_back() when you specifically want to remove the last element. Use erase() when you need to remove an element at another position or remove a range of elements.

OperationTypical useTime complexity
pop_back()Remove the final elementConstant time
erase(iterator)Remove an element at a specified positionLinear in the number of elements shifted after the erased position

C++ std::vector does not provide a pop_front() member. Removing the first vector element with erase(begin()) requires the later elements to be shifted.

C++ Vector pop_back() and Vector Capacity

pop_back() decreases the vector’s size, but it does not reduce its capacity. Capacity represents the amount of storage currently allocated for elements, so removing one element does not by itself release that allocation.

</>
Copy
#include <iostream>
#include <vector>
using namespace std;

int main() {
    vector<int> nums = {1, 2, 3, 4};

    cout << "Size before: " << nums.size() << '\n';
    cout << "Capacity before: " << nums.capacity() << '\n';

    size_t capacityBefore = nums.capacity();
    nums.pop_back();

    cout << "Size after: " << nums.size() << '\n';
    cout << boolalpha
         << "Capacity unchanged: "
         << (nums.capacity() == capacityBefore);

    return 0;
}

The exact numeric capacity may vary by implementation, but the comparison prints true because pop_back() does not shrink the vector’s capacity.

Common C++ vector::pop_back() Mistakes

  • Calling pop_back() on an empty vector: check empty() when emptiness is possible.
  • Expecting a returned value: use back() before pop_back() if you need the element.
  • Trying to remove an element by index: pop_back() only removes the last element; use erase() for another position.
  • Using an iterator or reference to the erased element afterward: it becomes invalid once that element is removed.

C++ Vector pop_back() Summary

In this C++ Tutorial, we learned how to remove the last element of a vector using pop_back() function.

Use vector::pop_back() when you want to remove exactly one element from the end of a non-empty vector. The function runs in constant time, reduces size() by one, does not return the removed value, and does not reduce the vector’s capacity.