Std :: map :: rend () returns the first element of the array, not the element before

I am trying to change the iteration through std :: map, following this code: http://www.cplusplus.com/reference/stl/map/rend/ It says:

rend () returns the inverse iterator, accessing the element just before the first element in the map container, which is considered its opposite end.

Note that rend does not apply to the same element as begin, but to the element directly in front of it.

map<float,int> m;
m.insert(pair<float,int>(.1,0));
m.insert(pair<float,int>(.4,5));
map<float,int>::reverse_iterator rend=m.rend();
map<float,int>::iterator begin=m.begin();

When I ran this, both expand and start with the first element m, (.1,0), when obviously this shouldn't, given the above. I feel that I see a very clear mistake, but I can’t understand what it can be.

(C ++, MSVC2010)

+3
source share
3 answers

, , , , rend() begin(), . operator* - .

: , rend(), begin(), . , rend() begin(), . operator* .

24.5.1. :

- _ - -, , , . : &*(reverse_iterator(i)) == &*(i - 1).

+3

, rend, - undefined . . , ( end ).

+2

, , rend, , .

int main() {

    map<float,int> m;
    m.insert(pair<float,int>(.1,0));
    m.insert(pair<float,int>(.4,5));
    map<float,int>::reverse_iterator rend=m.rend();
    map<float,int>::iterator begin=m.begin();

    for ( rend=m.rbegin() ; rend != m.rend(); rend++ )
    cout << rend->first << " => " << rend->second << endl;
}

:

0.4 => 5
0.1 => 0

rend , 0.1 = > 0 .

This is my example. You must show that your code, where you actually came to the conclusion, that rend points to the first element.

+2
source

All Articles