14

I'm not sure how to cast a value obtained from the pop_back() function of vector. The following is a simple code to illustrate the problem.

#include<vector>
#include<iostream>
using namespace std;
int main()
{
 vector<int> a,b;
 int val;
 a.push_back(1);
 a.push_back(2);
 a.push_back(3);
 a.push_back(4);
 for(int i=0; i<4; i++)
 {
 val = a.pop_back();
 b.push_back(val);
 }
 vector<int>::iterator v = b.begin();
 while( v != b.end())
 {
 cout << *v << " ";
 v++;
 }
 return 0;
}

This is the error I get.

pushback.cpp:18:9: error: assigning to 'int' from incompatible type 'void'
 val = a.pop_back();
 ^ ~~~~~~~~~~~~
1 error generated.

I tried casting as well; (int)a.pop_back(), but it throws an error stating C-style cast from 'void' to 'int' is not allowed.

May I know if there is a standard way to store the value from the pop_back() function?

asked Nov 9, 2016 at 5:56

5 Answers 5

30

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();
answered Nov 9, 2016 at 5:58
Sign up to request clarification or add additional context in comments.

1 Comment

auto val = a.back(); Does it copy the value? So there is one extra operation.
6

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();
answered Nov 9, 2016 at 5:59

2 Comments

Oh! Thank you for the clarification. I just got confused between the different languages (Python, Java, etc.) on how pop was implemented. It never crossed my mind that it was a misnomer in C++.
@ÉbeIsaac there is a reason for that, for example what to do if copy or move ctor throws exception?
3

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.

answered Feb 22, 2021 at 18:01

Comments

1

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

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

answered Nov 9, 2016 at 5:58

Comments

0

Or Use the .at(i) operator to return the value. For example

for(int i=0; i<4; i++)
{
 val = a.at(i); 
 b.push_back(val);
}
answered Dec 4, 2018 at 11:54

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.