C++ std::string erase() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::string

Definition

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);
CallWhat 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.

Library declarations
C++
string& erase(size_type pos = 0, size_type n = npos);
iterator erase(const_iterator position);
iterator erase(const_iterator first, const_iterator last);

Parameters

ParameterDescription
pos, nStart index and count.
iteratorsSingle position or [first, last) range.

Return Value

Return ValueDescription
string& or iteratorIndex form returns *this; iterator forms return the element after the erased part.

C++ Examples

Example 1 — Basic erase()

Remove three characters from the middle.

C++
#include <iostream>
#include <string>

int main() {
    std::string s = "abXXXcd";
    s.erase(2, 3);
    std::cout << s << '\n';
    return 0;
}

How It Works

  1. Index 2 starts at the first 'X'.
  2. Three characters are removed.
  3. "cd" shifts left to form "abcd".

Example 2 — Erase One Iterator

Erase the character at begin()+1.

C++
#include <iostream>
#include <string>

int main() {
    std::string s = "aXc";
    s.erase(s.begin() + 1);
    std::cout << s << '\n';
    return 0;
}

How It Works

  1. Iterator points at 'X'.
  2. erase removes that one character.
  3. Left with "ac".

Example 3 — Erase From Index to End

Drop a suffix.

C++
#include <iostream>
#include <string>

int main() {
    std::string s = "file.txt.bak";
    s.erase(8);
    std::cout << s << '\n';
    return 0;
}

How It Works

  1. Calling erase(8) uses default count npos.
  2. Everything from index 8 to the end is removed.
  3. Suffix ".bak" disappears.

Frequently Asked Questions

It removes characters and shifts the remainder left.
It erases through the end of the string.
The index overload throws std::out_of_range.
s.erase(i, 1) or s.erase(s.begin() + i).
clear removes everything; erase can remove a part.

Next: find()

Keep going through the string functions. Search for a substring or character; returns index or npos.

find() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful