s.erase in c++ code example
Example 1: remove or erase first and last character of string c++
str.pop_back();
str.erase(str.begin());
Example 2: removing a character from a string in c++
#include<iostream>
#include<algorithm>
using namespace std;
main() {
string my_str = "ABAABACCABA";
cout << "Initial string: " << my_str << endl;
my_str.erase(remove(my_str.begin(), my_str.end(), 'A'), my_str.end());
cout << "Final string: " << my_str;
}
Example 3: erasing a character from a string in c++
#include <iostream>
#include <string>
int main ()
{
std::string str ("This is an example sentence.");
std::cout << str << '\n';
str.erase (10,8);
std::cout << str << '\n';
str.erase (str.begin()+9);
std::cout << str << '\n';
str.erase (str.begin()+5, str.end()-9);
std::cout << str << '\n';
return 0;
}
Example 4: c++ erase remove
std::vector<int> v = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
v.erase(std::remove(v.begin(), v.end(), 5), v.end());