Could anyone please tell me how to get the second last element of the vector since back() returns the last element
Thanks
vec.rbegin() - 1
returns an iterator to the second last element, as long as there is one.
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
YES it worked with +1
MANY THANKS :)