Except using rbegin, are there any other stl functions for getting the iterator of the last element? vector::back just gives me the reference of that.
Printable View
Except using rbegin, are there any other stl functions for getting the iterator of the last element? vector::back just gives me the reference of that.
--c.end().
Only works for Bidirectional containers but, then, so does c.rbegin().
Graham,
Isn't c.end () one past the last one?
That's why I applied the decrement operator to it.
Sorry - I thought the -- was a comment. Ada is getting me confused.
If i use decrement operator on the end iterator, will the end iterator "itself" decrease by 1?
Just like
int i = 0;
--i;
Then i will become -1.
May be this is a silly question, sorry. :(
No.
end() simply returns a one-past-the-end iterator and it's that that you decrement.
It would probably be a good idea to check that the container is not empty(), though. Decrementing end() then could cause problems.Code:std::vector<int> v;
// fill v...
std::vector<int>::iterator i = --v.end();
// i now points to the last element in v.
Oops, VC6 can't compile it.
error C2105: '--' needs l-value
OK. Get the interator into a variable and decrement that:
Code:(whatever)::iterator i = c.end();
--i;
Thanks:)
You can also writefor vector iterators.Code:(c.end() - 1)