1#include<iostream>
2#include<algorithm>
3
4using namespace std;
5main() {
6 string my_str = "ABAABACCABA";
7
8 cout << "Initial string: " << my_str << endl;
9
10 my_str.erase(remove(my_str.begin(), my_str.end(), 'A'), my_str.end()); //remove A from string
11 cout << "Final string: " << my_str;
12}
1#include <iostream>
2#include <algorithm>
3#include <string>
4
5int main()
6{
7 std::string s = "This is an example";
8 std::cout << s << '\n';
9
10 s.erase(0, 5); // Erase "This "
11 std::cout << s << '\n';
12
13 s.erase(std::find(s.begin(), s.end(), ' ')); // Erase ' '
14 std::cout << s << '\n';
15
16 s.erase(s.find(' ')); // Trim from ' ' to the end of the string
17 std::cout << s << '\n';
18}
1 string& erase (size_t pos = 0, size_t len = npos);
2/*
3pos
4Position of the first character to be erased.
5If this is greater than the string length, it throws out_of_range.
6Note: The first character in str is denoted by a value of 0 (not 1).
7len
8Number of characters to erase (if the string is shorter, as many characters as possible are erased).
9A value of string::npos indicates all characters until the end of the string.
10*/