Needing tip with multiple deque.erase

Hi everyone! I'm working on a little project, that in general terms, interacts with a .csv file with two columns (it's a kind of dictionary, so let's say word and translation). Everytime a class is called, in the constructor I open the file, read each line, and put it in a deque of structs of two strings.

One of my program operations is to erase some content of my file, and I achieve that with the function my_deque.erase (and then in the destructor rewritting everything into my file, of course). Now:

I wanted to give the option to multiple deleting, by writing the different numbers of the entries to be deleted separated with commas (i.e. '14,255,366'), and then saving each number in a integer's vector.

Now I'm facing the fact that, after the first my_deque.erase, all bigger iterators will be changed, thus not maching with the ones the user has chosen.

After some trial and error, I've write this simple code:

1
2
3
4
5
int t=0;  // each time I erase, the iterators will be (-1).
for (i=0; i<int_vector.size (); i++) {
my_deque.erase ( my_deque.begin() + (int_vector[i] - (1+t)) ); //the 1 is because the user is seeing a list of the entries and it starts at 1
t++;
}


As you can deduce, it will work as long as the user inputs the numbers in order (from smaller to bigger). But, of course, you can't expect that much from users, so I'm wondering if you have any idea, suggestion, on how to make it work if numbers aren't in order. (Or if you just have a better way to do the all process!)

Thank you!

J.
Last edited on
deque isn't the right tool for the job here. Use std::map. You can put a unique ID for each word, and use this ID for all operations like deleting.

http://www.cplusplus.com/reference/stl/map/
Last edited on
MMmnn, looking nice! Thank's for the tip. ;)
You're welcome :)
Topic archived. No new replies allowed.