Popping the last item in a std::vector

J

James Brown

Hi,
I am using the std::vector class as follows:

vector <myclass *> stack1;

and am pushing myclass objects onto the end of the
vector like so:

myclass *ptr = new myclass();
stack1.push_back(ptr);

what I would like to do is pop this item from the end of
the vector as well, however the vector::pop_back method does not
return anything (it has a void return-type). So my question is, what
is the preferred, neatest method to pop the last element from a vector
but also retain that value?

maybe iterators or direct-access using the operator[], followed by the
pop_back call?
Basically I'd like to know what the most common method is to do this...


Thanks,
James
 
R

Rolf Magnus

James Brown said:
Hi,
I am using the std::vector class as follows:

vector <myclass *> stack1;

and am pushing myclass objects onto the end of the
vector like so:

myclass *ptr = new myclass();
stack1.push_back(ptr);

What about std::stack?
what I would like to do is pop this item from the end of
the vector as well, however the vector::pop_back method does not
return anything (it has a void return-type). So my question is, what
is the preferred, neatest method to pop the last element from a vector
but also retain that value?

myclass* ptr = stack1.back();
stack1.pop_back();
 
M

Mike Wahler

James Brown said:
Hi,
I am using the std::vector class as follows:

vector <myclass *> stack1;

and am pushing myclass objects onto the end of the
vector like so:

myclass *ptr = new myclass();
stack1.push_back(ptr);

what I would like to do is pop this item from the end of
the vector as well, however the vector::pop_back method does not
return anything (it has a void return-type). So my question is, what
is the preferred, neatest method to pop the last element from a vector
but also retain that value?

if(!stack1.empty())
{
myclass *p = *(stack1.end()-1);
stack1.pop_back();
}

IMO 'stack1' isn't a very good name for a vector.
The standard library does have a 'stack' container,
perhaps you want to use that instead.

-Mike
 
M

msalters

James said:
Hi,
I am using the std::vector class as follows:

vector <myclass *> stack1;

and am pushing myclass objects onto the end of the
vector like so:

myclass *ptr = new myclass();
stack1.push_back(ptr);

what I would like to do is pop this item from the end of
the vector as well, however the vector::pop_back method does not
return anything (it has a void return-type). So my question is, what
is the preferred, neatest method to pop the last element from a vector
but also retain that value?

There is intentionally no such method. The reason is exception-safety,
which is relevant whenever vector holds elements whose copy ctor
can throw. The bad scenario is: vector<T>::pop_back copies the last
element into a temporary (OK), destroys the last object of the array
(OK, shouldn't throw), and then tries to return the temporary(THROWS).
Now the last object is lost forever. That's bad.

Your case is different, because you know pointer copies won't throw.
So you can use that specialized knowledge to implement a free function
doing exactly what I described.

Regards,
Michiel Salters
 
M

msalters

James said:
Hi,
I am using the std::vector class as follows:

vector <myclass *> stack1;

and am pushing myclass objects onto the end of the
vector like so:

myclass *ptr = new myclass();
stack1.push_back(ptr);

what I would like to do is pop this item from the end of
the vector as well, however the vector::pop_back method does not
return anything (it has a void return-type). So my question is, what
is the preferred, neatest method to pop the last element from a vector
but also retain that value?

There is intentionally no such method. The reason is exception-safety,
which is relevant whenever vector holds elements whose copy ctor
can throw. The bad scenario is: vector<T>::pop_back copies the last
element into a temporary (OK), destroys the last object of the array
(OK, shouldn't throw), and then tries to return the temporary(THROWS).
Now the last object is lost forever. That's bad.

Your case is different, because you know pointer copies won't throw.
So you can use that specialized knowledge to implement a free function
doing exactly what I described.

Regards,
Michiel Salters
 
M

Mike Wahler

Victor Bazarov said:
Mike Wahler said:
[...]
IMO 'stack1' isn't a very good name for a vector.
The standard library does have a 'stack' container,

Ahem... 'stack' container _adaptor_.

Um, I'll try to wiggle out: 'stack' type.

You just like to pick on me, huh? :)

-Mike
 
V

Victor Bazarov

X-No-archive: yes
Mike said:
Mike Wahler said:
[...]
IMO 'stack1' isn't a very good name for a vector.
The standard library does have a 'stack' container,

Ahem... 'stack' container _adaptor_.


Um, I'll try to wiggle out: 'stack' type.

You just like to pick on me, huh? :)

Hey, I tried picking on girls, it didn't work very well, and
you were just there... So... :))
 
J

James Brown

Thankyou all for your replies, in my ignorance
I failed to find the vector::back method, and wasn't aware
of the std::stack implementation....this certainly looks like the
best solution for my needs.

Thanks,
James

Please de-spam my email address before replying.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,197
Messages
2,571,040
Members
47,635
Latest member
SkyePurves

Latest Threads

Top