Marcel Müller said:
Since std::set has no definition of a sequence, there is of course no way
to step n elements forward or backward.
Without the definition of a sequence, you could not even have an iterator
at all.
Of course you _can_ move n steps forward or backward, simply by calling
operator++/-- n times.
You could even define operator+ yourself in terms of operator++. I would
not recommend it though (for the reasons see below).
Conceptially, a set has no sequence, that's true. But by iterating over a
specific instance of it, you actually creating one.
A set is a set without any reproducible element ordering.
A std::set _is_ (weakly) ordered. If a < b then a comes before b.
You can not even be sure to get the elements in the same order twice.
You can. At least for the same instance of the std::set this is guaranteed
(if you don't change it in between of course).
For the most definitions of operator<, this also holds for different
instances containing the same values.
If you need a sequence, i.e. a container with some defined element
ordering, std::set is not your choice.
If you want an unordered set, take std::unordered_set from C++11. std::set
is ordered.
But even if you have a sequence, moving iterators many places might not
be that cheap. Think of simple, binary tree implementations.
That's one good reason against operator+. It would only obfuscate the fact
that it has to be implemented via operator++ in the general case.
Another reason is, that you can possibly get out of bounds. Since the
iterators themselfs don't have to be ordered, you cannot check (it <
set.end()) but have to check (it != set.end()). And since ++set.end() is
undefined, there's no way you can ensure that you won't get out of bounds.
Tobi