2

I want to remove the last element from vector v and copy it to variable q. Is there a more concise way of doing it than this:

vector<int> v{3, 1, 4, 1};
int q = v.back();

v.pop_back();
Paul Jurczak
  • 7,008
  • 3
  • 47
  • 72
  • 1
    `Why pop_back() doesn't return the value or reference to removed element?` Well, it obviously can't return a reference - what storage would it refer to, seeing as the element has been removed? As to why it doesn't return a value - it can't do that while providing strong exception safety guarantee; it can't keep the vector intact If `T`'s copy-constructor throws while attempting to return the value. – Igor Tandetnik Feb 01 '16 at 03:42
  • @Igor Thanks. Of course, returning a reference would not make sense. I removed that question, since it is a duplicate and stack overflow police already got on my case. – Paul Jurczak Feb 01 '16 at 04:03

0 Answers0