How to store a value obtained from a vector `pop_back()` in C++?

As stated in documentation std::vector::pop_back() does not return any value, you just need to call std::vector::back() right before:

val = a.back();
a.pop_back();

It may sound as pop as in returning a value. But it actually doesn't. The standard says that vector::pop_back should erase the last value, with no return value.

You can do:

auto val = a.back();
a.pop_back();

According to http://www.cplusplus.com/reference/vector/vector/pop_back/

The pop_back is void function, it is nonvalue-returning.


In case you have a vector of objects and not just primitive types, moving out from the vector can be done with std::move:

auto val = std::move(a.back()); // allow call of move ctor if available
a.pop_back();

Note that wrapping the call to back() with std::move is done only because we know that we are about to erase this element from the vector on the next line.