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// string::erase
2#include <iostream>
3#include <string>
4
5int main ()
6{
7 std::string str ("This is an example sentence.");
8 std::cout << str << '\n';
9 // "This is an example sentence."
10 str.erase (10,8); // ^^^^^^^^
11 std::cout << str << '\n';
12 // "This is an sentence."
13 str.erase (str.begin()+9); // ^
14 std::cout << str << '\n';
15 // "This is a sentence."
16 str.erase (str.begin()+5, str.end()-9); // ^^^^^
17 std::cout << str << '\n';
18 // "This sentence."
19 return 0;
20}
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}