In C++, std::string::erase()deletes characters from the string. Later characters shift left to close the gap.
Include <string>.
This is a member of std::string. A string grows as you append, stores characters contiguously, and can reallocate—after growth, old iterators and references may be invalid. Prefer at() when you want bounds checks; remember find returns npos when missing.
Syntax
Common forms:
C++
s.erase(2, 3); // from index 2, remove 3 chars
s.erase(s.begin() + 1); // remove one character
s.erase(s.begin(), s.begin() + 2);
Call
What it does
s.erase(pos, n)
Erase up to n characters starting at index pos.
s.erase(it)
Erase the character at iterator it.
s.erase(first, last)
Erase the half-open iterator range.
If pos > size(), the index overload throws std::out_of_range. If n is too large, it erases through the end.