Consider the following code for example.
string str = "Alice ate apples";
str.erase(0, 2)
Does erase function actually allocate new memory and copy the "ice ate apples" or does erase function do an in-place copy?
It is illegal for a basic_string implementation to have the iterator forms of erase throw exceptions. And even the index form of erase only throws when you provide an out-of-range index.
That's important because allocating memory is a potentially throwing operation. So if erase cannot throw, then it cannot allocate either. So it doesn't.
Therefore, the erasure must happen in-place.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With