...
Do not use an invalidated reference, pointer, or iterator because doing so results in undefined behavior.
This Since std::basic_string
is a container of characters, this rule is a specific instance of CTR51-CPP. Use valid references, pointers, and iterators to reference elements of a container.
...