std::string::erase in C++
Last Updated :
20 Nov, 2024
The std::string::erase() function is a built-in function of std::string class that is used to erase the whole or a part of the string shortening its length. In this article, we will learn how to use string::erase() function.
Syntax
The std::string::erase() function provides 6 different overloads for different purposes:
s.erase() // Erases whole string
s.erase(idx) // Erases all characters after idx
s.erase(idx, k) // Erases k characters after idx
s.erase(itr) // Erases character at itr
s.erase(first, last) // Erases character in range [first, last)
These 5 overloaded versions of the string::erase() function can be used for following purposes:
Erase Single Character from Given Position
string::erase() function can be used for erasing a single character from the specific position in the string.
Syntax
s.erase(itr)
Parameters
- itr: Iterator to the character you want to erase (note that it is an iterator, not index)
Return Value
- Returns the character now at the position of the removed character.
- If no such character is remaining, returns iterator to the string::end()
To learn more about string manipulation and other useful functions, our Complete C++ Course offers lessons on handling strings, arrays, and dynamic data efficiently in C++.
Example
C++
// C++ Program to illustrate how to use string erase
// to remove a character from specific position
#include <iostream>
#include <string>
using namespace std;
int main() {
string s("Hello World!");
// Deletes character at position 4
s.erase(s.begin() + 4);
cout << s;
return 0;
}
Erase Range of Characters
We can also erase a range of characters from the string using string::erase() function.
Syntax
s.erase(first, last)
Parameters
- first: Iterator to the first element in the range.
- last: Iterator to one element after the last element in the range.
Return Value
- Returns the character now at the position of the first removed character.
- If no such character is remaining, returns iterator to the string::end()
Example
C++
// C++ Program to illustrate how to use the string
// erase function to remove a range of characters
#include <bits/stdc++.h>
using namespace std;
int main() {
string s("Hello World!");
// Define the range as the word "Hello "
auto first = s.begin();
auto last = s.begin() + 6;
// Remove the range [first, last)
s.erase(first, last);
cout << s;
return 0;
}
Erase k Characters After Given Position
The below version erases all the characters after the given index, but we can also limit the number of characters to be deleted using string::erase.
Syntax
s.erase(idx, k)
Parameters
- idx: The position (index) at which character deletion begins.
- k: Number of characters to be erased (including character at idx).
Return Value
- This version returns the current string.
Example
C++
// C++ program to illustrate the use of string::erase()
// to delete specified number of characters after given
// index
#include <bits/stdc++.h>
using namespace std;
int main() {
string s("Hello World!");
// Deletes 5 characters starting from index number 0
s.erase(0, 5);
cout << s;
return 0;
}
Erase All Characters After Given Position
string::erase() function can also be instructed to erase the characters after some given position.
Syntax
s.erase(idx)
Parameters
- idx: The position (index) at which character deletion begins.
Return Value
- This version returns the current string.
Example
C++
// C++ program to illustrate how to use string::erase() to delete
// the characters after the given characters.
#include <bits/stdc++.h>
using namespace std;
int main() {
string s("Hello World!");
// Deletes the word " World!"
s.erase(5);
cout << s;
return 0;
}
Erase All Characters
The string::erase() function is able to delete all the characters of the string effectively clearing it. The length of the string will be reduced to 0.
Syntax
s.erase()
Parameter
- This version takes no parameters.
Return Value
- This version returns the current string.
Example
C++
// C++ code to illustrate how to use string::erase()
// to clear the string
#include <bits/stdc++.h>
using namespace std;
int main() {
string s("Hello World!");
// Erasing string using string::erase()
s.erase();
cout << s;
return 0;
}
Output
(no output)
string::erase() vs string::clear()
The string::clear() is a function used to erase the contents of the string, but it is a lot different from string::erase(). Below is the table that lists the major differences between string::erase() and string::clear().
| Aspect | string::erase() | string::clear() |
|---|
| Purpose | Removes specific characters or a range of characters. | Removes all characters from the string. |
|---|
| Syntax | erase(position); erase(first, last); | clear(); |
|---|
| Parameters | – Position of a single character – Range of characters (two iterators) | None |
|---|
| Returns | Iterator pointing to the position after the removed range | None |
|---|
| Time Complexity | – Single character: O(m), where m is the number of characters after the position. – Range: O(m +k), where k is the size of the range. | O(1), where n is the number of characters in the string. |
|---|
| Space Complexity | O(1), as it modifies the string in place. | O(1), as it clears the string in place. |
|---|