As stated in the comments, the standard doesn't specify any complexity requirements for many basic_string
functions (including erase
). This is partly because there have historically been a number of different implementation strategies (most famously, copy-on-write was popular in the C++98 era), so the committee was reluctant to specify anything too precisely.
The basic behavior of typical, modern implementations is very much like vector<char>
: insertion and deletion are cheap at the end (with amortized reallocations) and expensive at the beginning. Small strings are handled without memory allocation at all (by reusing the pointers' space to store the characters). This means that erasure can result in copying the whole string if it becomes very short (but then the copy is cheap). It also means that iterators and references are invalidated more easily than with vector
.
There aren't any algorithms that perform better on a string
, but there are alternate data structures that have different performance characteristics. The rope is typical among these in that it provides somewhat slower access but much faster insertion and deletion.