The reverse iterator does not work as expected

I use the reverse iterator on std::vector and at the following link:

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

myVector.rbegin() is the last element of the vector. In my case, I actually get the past the end iterator on rbegin() and the first element from rend() . I would expect rend() give me a tetra past the end and rbegin() to give me the last element in the container. I realized that all this is wrong?

Below is my code, nothing special. I set a breakpoint immediately after the assignments, and above is the result that I get in the debugger ( VecDebugCubes is a type definition for std::vector<myStructure> )

 VecDebugCubes::reverse_iterator itr = pActiveDebugCubes.rbegin(); VecDebugCubes::reverse_iterator itrEnd = pActiveDebugCubes.rend(); while (itr != itrEnd) { (*itr)->printDebugValues(); ++itr; } 
+4
source share
2 answers

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

Dereferencing an inverse iterator returns a different value than the one you see in your debugger.

+5
source

for the end for rbegin (), and the first element for rend () is how the iterator should work. That's why he called the reverse iterator. It is designed in such a way that you can use it in a for loop without worrying about making an error regarding the start and end points of the range. You will notice that simple and ++ can be used for an iterator, and the iterator actually performs a range check again. I think all you need to know is how it should work. There is nothing bad. It just simplifies your life, as you are less likely to make a mistake when writing for (i = beginAtTheEnd to complete AtTheStart and decment-i-in-the-same-way-as-i ++) rather than (i = 10; i > 0; I -) , where you can make a mistake when setting I <0 or i> 1 in state. This is a simplified example. You will appreciate it when you want to flip the container iteration.

0
source

All Articles