Nov 9, 2010 at 2:25pm
Could anyone please tell me how to get the second last element of the vector since back() returns the last element
Thanks
Nov 9, 2010 at 2:31pm
vec.rbegin() - 1
returns an iterator to the second last element, as long as there is one.
Nov 9, 2010 at 3:11pm
Well, as I said, it returns an iterator to the element, not the element itself. You must dereference it:
*(vec.rbegin() - 1)
EDIT: actually, since it's a reverse iterator, I think it's +1 instead:
*(vec.rbegin() + 1)
Last edited on Nov 9, 2010 at 3:13pm
Nov 9, 2010 at 3:20pm
YES it worked with +1
MANY THANKS :)