Skip to main content
gamedev.net
🔒 Locked

std::vector[vector.size() - 1]

Started by daniel_i_l Aug 16, 2006 at 2:10 PM 9 replies 2k views
Original Post
daniel_i_l
daniel_i_l
If I have a std vector of variables, for example: std::vector vec; how do I get the last one - the equilivant to: vec[vec.size()-1] ? I remember reading that there was a better way. Thanks.
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky
dave
dave
myVector.end() - 1;
Conner McCloud
Conner McCloud
Quote:
Original post by Dave
myVector.end() - 1;

vector::end() returns an invalid iterator. You can't legally use it to reach a valid one.

*edit: Ignore me, I have been living a lie.

CM

[Edited by - Conner McCloud on August 16, 2006 2:29:04 PM]
dave
dave
Quote:
Original post by Conner McCloud
Quote:
Original post by Dave
myVector.end() - 1;

vector::end() returns an invalid iterator. You can't legally use it to reach a valid one.

CM


So that's why none of my code ever works. Bugger.

I'M A NOOB!
Evil Steve
Evil Steve
My code is a mess. I used to use vec.end[-1], then I discovered vec.back() [smile]

Out of interest (Because I'm too lazy to look for myself), what does std::vector do if you call back() and it's empty? The same as accessing an invalid index with operator[] or .at()?
Bregma
Bregma
Quote:
Original post by Evil Steve
Out of interest (Because I'm too lazy to look for myself), what does std::vector do if you call back() and it's empty? The same as accessing an invalid index with operator[] or .at()?


a.back() returns *--a.end() for vector a [ISO/IEC 14882:1998(E) 23.1.1 Table 68]. If the vector is empty, the results will be undefined since in that case a.begin == a.end(), and one-before-the-beginning of a vector is undefined.
Stephen M. Webb
Professional Free Software Developer
Polymorphic OOP
Polymorphic OOP
Quote:
Original post by Conner McCloud
Quote:
Original post by Dave
myVector.end() - 1;

vector::end() returns an invalid iterator. You can't legally use it to reach a valid one.

CM


That's not true. It's perfectly fine to go from end to another iterator through iterator operations. This holds for all container types.
Conner McCloud
Conner McCloud
Quote:
Original post by Dave
Quote:
Original post by Conner McCloud
Quote:
Original post by Dave
myVector.end() - 1;

vector::end() returns an invalid iterator. You can't legally use it to reach a valid one.

CM


So that's why none of my code ever works. Bugger.

I'M A NOOB!

In that case, it probably does work. Which is why its so important not to get into the habit of doing it. A more complex structure [say, std::set] may not work that way.

*edit: Or maybe I'm mistaken...

CM
etothex
etothex
I would consider vector::end()-1 to be suspect code as well, but it seems that microsoft doesn't have a problem with it.
Conner McCloud
Conner McCloud
Quote:
Original post by Polymorphic OOP
Quote:
Original post by Conner McCloud
Quote:
Original post by Dave
myVector.end() - 1;

vector::end() returns an invalid iterator. You can't legally use it to reach a valid one.

CM


That's not true. It's perfectly fine to go from end to another iterator through iterator operations. This holds for all container types.

I stand corrected, and have edited my post accordingly. I was under the impression that i-- required i be dereferenceable, but apparently it does not.

CM

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.