I want to remove current character and previous character in vector if it matches with the specified character. The code works for alphabetic characters like a, b, c. But if i want to remove multiple characters like *, the code gives segmentation fault. Any suggestions?
#include<iostream>
#include <vector>
#include<string>
using namespace std;
int main(){
vector<char> v;
vector<char>::iterator it;
string s= "abcdef*gh*i";
for(int i=0; i<s.size(); i++){
v.push_back(s[i]);
cout << v[i] <<" ";
}
cout <<"n";
for (it = v.begin(); it!= v.end(); it++)
{
if(*it == '*'){
v.erase(it);
v.erase(it-1);
}
}
for(int i=0; i< v.size(); i++)
cout << v[i]<<" ";
return 0;
}
Result: Seg Fault