Hi,
What I'm trying to do is to remove elements from my vector. Generally the idea is to do something like this:
Code:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int main (){
vector<string> vec;
for (int i =0; i< 10; i++)
vec.push_back("abcdefghijk"+to_string((long long)i));
for(int k = 0 ; k< vec.size();k++){ //bla bla bla
if(k==5 || k==7)
vec.erase(vec.begin()+k);
}
for (int i =0; i< vec.size(); i++)
cout << vec[i] << endl;
return 0;
}
but in this case each time i remove an elament i need to recompute the index of the next element. So let say I want to remove the 5th and 7th element. the above algorithm will remove 5th with no problem but the 7th is not 7th any more but 6th. is there a clean way to do this without me explicitly recomputing the index position in each cycle. also is there a faster way to do this ?