1

I have a vector index which contains the indices of elements that I want to remove from the vector words.

vector<int> Index; vector<int> words; 

I tried using this method, but my program crashes during runtime why? What is happening?

for(int t1 = 0; t1 < index.size(); t1++) { words.erase(words.begin()+ index[t1]) } 

Thank you.

1
  • Problem: Your word vector gets updated after every erase(size, iterators etc), but not your index vector, so every subsequent iteration remove the undesired element and at one instance index vector return the index that is not present in Word vector. Solution: you can find it in below posted answer. Please accept the answer which satisfied you need. Commented Jun 14, 2013 at 10:45

4 Answers 4

3

It is hard to tell exactly why your program crashes without seeing how you declare and initialize words and index.

However, what is most likely to happen is that after removing an element from words, and after all the subsequent elements have been shifted to the left by one position, the indices in index may index positions which are beyond the new end of the vector.

When i is greater than the size of the vector, evaluating words.begin() + i will result in undefined behavior (which in your case is manifesting itself as a crash).

If your vector of indices is sorted in increasing order, just revert your loop:

for(int t1 = index.size() - 1; t1 >= 0; --t1) { words.erase(words.begin() + index[t1]); } 

Or alternatively, you can use your original loop and sort the indices in decreasing order.

Sign up to request clarification or add additional context in comments.

1 Comment

vector<int> words is a vector of integers vector<int> index contains the indices of the integers that i must remove from words Step 1: I sort the vector Index sort(index.begin(),index.end()); Step2: I loop through words to remove the elements
2

The problem here is that you are updating the size of the vector when you are erasing some elements in it.

Try this instead:

for(int t1 = index.size()-1; t1 >= 0; --t1) { words.erase(words.begin()+index[t1]) } 

11 Comments

vector<int> words is a vector of integers vector<int> index contains the indices of the integers that i must remove from words Step 1: I sort the vector Index sort(index.begin(),index.end()); Step2: I loop through words to remove the elements
@HaniGoc: hmm, so the problem is a bit more complicated, when you erase an element of the vector, it rearrange the content of the vector.
words.erase(words[index[t1]]) Error: No matching function for call to std::vector<int>::erase(int&)
Oh I see, what you mean is that i should decrement the indices vector<int>Index?
@Kevin Maybe you should elaborate the problem a bit more in your answer. It's clear for someone who has (more than one time ;-) ) tripped over this: Indices change if you delete elements from a vector, but only those indices get invalid that referenced an element successing the erased element. So erasing by reverse iterating is save, while forward iterating needs additional effort.
|
2

Try performing the operation in reverse. Subsequent indices are going to become invalid once you delete a later index.

I have assumed that your indices are ordered. If not then order them and then ensure you start by removing the largest index first.

Comments

0
vector<int>a = {0,0,0,0,1,1,1,1,0,0,0,0,1,0,0,0,0,0,0}; vector<int>index; int t = 0; while(t < a.size() && a[t] != 1) { index.push_back(t); t = t + 1; } t = a.size()-1; while( t > 0 && a[t] != 1) { index.push_back(t); t = t - 1; } sort(index.begin(),index.end()); index.erase(std::unique(index.begin(), index.end()), index.end()); cout << "Before: "; for(int i = 0; i <a.size();i++) { cout << a[i] <<" "; } cout << endl; int counter = 0; for(int i = 0; i < index.size();i++) { a.erase(a.begin() + (index[i]-counter)); counter = counter + 1; } cout <<"After: "; for(int i = 0; i < a.size();i++) { cout << a[i] <<" "; } 

2 Comments

As You Can see in here, I had to use the counter to keep the index vector Updated. I don't know i hope to find a more easier solution. Thank you everyone for your help.
I find it quite hard to understand, what this code ever does what it is meant for. Frankly, I would stick to Kevins solution.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.