From section 23.2.1 [container.requirements.general]
:
Unless otherwise specified (either explicitly or by defining a function in terms of other functions), invoking a container member function or passing a container as an argument to a library function shall not invalidate iterators to, or change the values of, objects within that container.
For associative containers such as std::set
, section 23.2.4 ([associative.reqmts]
) says:
The insert
and emplace
members shall not affect the validity of iterators and references to the container, and the erase
members shall invalidate only iterators and references to the erased elements.
So your iterators will remain valid after inserting additional items.
However, thread safety is a different topic completely.
Section 17.6.5.9 ([res.on.data.races]
) provides that
Operations on iterators obtained by calling a standard library container or string member function may access the underlying container, but shall not modify it.
Since that results in reading the container while it's being updated, it is not necessarily safe to use a std::set
iterator while inserting into the collection from another thread. Your implementation may provide a stronger guarantee.