I have a map object to which I want to insert a new element. After doing this, I want to make sure that this newly inserted element was inserted at the end. To do this, I have devised the following way:
map<T1,T2> m;
//populate m
auto ret_pair = m.insert({o1,o2}); //o1,o2 objects of types T1,T2
auto end = m.cend();
--end;
if (ret_pair.first != end) //<-- this worries me
throw runtime_error("boom");
The line where I compare the two iterators worries me, because I’m not sure if I can rely on these different iterators to be the same, even if the point to the same thing.
What do you think? Why would this (not) work?
Iterators which point to the same element compare equal. If they didn’t, every algorithm in the standard library would break.
So yes, your code is fine.