References and Reverse Iteration over map

Hullo,

I had a simple question. I surfed through the internet and came upon this solution for reverse iterating a map:

1
2
3
4
for (auto it = map.rbegin(); it != map.rend(); it++) {
    it->first... // key
    it->second... // value
}

My question is that if I changed it->second to it->second->x += 2 or something, will it modify the actual object in the map or will auto it = map.rbegin() make a copy of the object?

Note: This is assuming that the map's value is a pointer type; std::map<int, Object*> map;
I strongly believe that this will change the actual object, I just want to confirm.


Last edited on
It will modify the object in the map. Example:
1
2
3
4
5
6
std::map<int, int> map;
map[4] = 5;
for (auto it = map.rbegin(); it != map.rend(); ++it)
    it->second = 2;
for (auto &kv : map)
    std::cout << kv.second << std::endl;
Output:
2
Thanks for the clarification!
Topic archived. No new replies allowed.