public member function
<vector>

std::vector::pop_back

void pop_back();
Delete last element
Removes the last element in the vector, effectively reducing the container size by one.

This destroys the removed element.

Parameters

none

Return value

none

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// vector::pop_back
#include <iostream>
#include <vector>

int main ()
{
  std::vector<int> myvector;
  int sum (0);
  myvector.push_back (100);
  myvector.push_back (200);
  myvector.push_back (300);

  while (!myvector.empty())
  {
    sum+=myvector.back();
    myvector.pop_back();
  }

  std::cout << "The elements of myvector add up to " << sum << '\n';

  return 0;
}

In this example, the elements are popped out of the vector after they are added up in the sum. Output:
The elements of myvector add up to 600

Complexity

Constant.

Iterator validity

The end iterator and any iterator, pointer and reference referring to the removed element are invalidated.
Iterators, pointers and references referring to other elements that have not been removed are guaranteed to keep referring to the same elements they were referring to before the call.

Data races

The container is modified.
The last element is modified. Concurrently accessing or modifying other elements is safe, although iterating ranges that include the removed element is not.

Exception safety

If the container is not empty, the function never throws exceptions (no-throw guarantee).
Otherwise, it causes undefined behavior.

See also