Assume that I have a string 'abcd', and a vector [4,1,3,2] to index the string. For example, first element of vector is 4, so I should remove 4th character in 'abcd' which refers to 'd'. Then the second element of vector is 1, so I should remove 1st character in 'abcd' which refers to 'a', and so on. I want to record the operated string every time I remove one character. And here is my code:
# include <iostream>
# include <vector>
using namespace std;
int main()
{
int m;
cin >> m;
string T;
cin >> T;
cout << T << endl;
vector<int> arr(m);
for(auto &x : arr) cin >> x;
// Remove character from string at given index position
for (int i = 0; i < m; i++){
T.erase(T.begin() + arr[i]-1);
cout << T << endl;
}
return 0;
}
However, I had faced some problem in the output, how could I fix it?
4
abcd
abcd
4 1 3 2
abc
bc
Segmentation fault