Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverse Iterator not working as expected

I am using a reverse iterator on a std::vector and according to the following link:

http://www.cplusplus.com/reference/stl/vector/rbegin/

myVector.rbegin() is the last element of the vector. In my case, I am actually getting the past the end iterator on rbegin() and the fist element from rend(). I would have expected rend() to give me past the end iterator and rbegin() to give me the last element in the container. Did I understand the whole thing wrong?

The following is my code, nothing special. I put a break point just after the assignments, and the above is the result I am getting in the debugger (VecDebugCubes is a type define for a std::vector<myStructure>)

VecDebugCubes::reverse_iterator itr    = pActiveDebugCubes.rbegin();
VecDebugCubes::reverse_iterator itrEnd = pActiveDebugCubes.rend();
while (itr != itrEnd)
{
  (*itr)->printDebugValues();
  ++itr;
}
like image 477
Samaursa Avatar asked Dec 04 '10 04:12

Samaursa


1 Answers

See http://www.cplusplus.com/reference/std/iterator/reverse_iterator/base/.

Dereferencing a reverse iterator will return a different value than the one that you see it pointing to in your debugger.

like image 119
Chris Hopman Avatar answered Oct 14 '22 01:10

Chris Hopman