Here, to avoid runtime errors, you would have had to reserve() enough capacity before r and p
were initialized.
References, pointers, and iterators that refer to characters of a string may be invalidated by the
following operations:7
• If the value is swapped with swap()
• If a new value is read by operator>>() or getline()
• If any nonconstant member function is called, except operator [ ], at(), begin(), end(),
rbegin(), and rend()
See Section 13.2.14, page 684, for details about string iterators.