*(it)
does not return an index. If it
is an iterator that references a member of the set, then *(it)
is equivalent to *it
(the ()
are not required). It therefore accesses the corresponding member of the set. If it
is a past-the-end iterator, the behaviour is undefined.
*(++it)
first increments it
, and then accesses the corresponding set element. So
cout << *(++it) << endl;
gives the same output as
++it;
cout << *it << endl;
which gives undefined behaviour if it
was already a past-the-end iterator OR if incrementing it gives a past-the-end iterator.
In your example, the s.upper_bound(x)
returns a past-the-end iterator, since x
is greater than any member of the set. Both output statements in your code therefore give undefined behaviour.