Consider the following code:
std::set<int> s;
auto it = s.begin();
s.insert(1);
s.insert(2);
std::cout << *it << std::endl;
The output (at least for me) is 2
. What's happening here? What's the state of it
when I dereference it?
I know that when I call begin()
on an empty set, I get an iterator equivalent to end()
. I also know that calling insert
on a set
doesn't invalidate its iterators. Does the iterator somehow stay equivalent to end()
even though I've now inserted elements into the set
and so now I'm getting undefined behaviour? Is that defined by the standard?