HaniDaher
asked on
runtime Error Erase element vector C++
Hello,
I have a
I want to use the indices in the vector index to remove the numbers in vector<int>numbers
Example:
I tried the reverse way also :
for(int t1 = index.size()-1; t1 > 0; --t1)
{
words.erase(words.begin()+ index[t1])
}
I also tried to decrement the values in the vector Index since every time i remove an element from vector numbers the index will decrease right?
So also it crashed
I have a
vector<int>Index = [1,3,4,5,8,9]
vector<int>numbers = [1,2,3,4,5,6,7,8,9,10]
I want to use the indices in the vector index to remove the numbers in vector<int>numbers
Example:
for(int t1 = 0; t1 < index.size(); t1++)
{
words.erase(numbers.begin()+ index[t1])
}
But my program keeps on crashing.I tried the reverse way also :
for(int t1 = index.size()-1; t1 > 0; --t1)
{
words.erase(words.begin()+
}
I also tried to decrement the values in the vector Index since every time i remove an element from vector numbers the index will decrease right?
So also it crashed
int counter = 0;
for(int t1 = index.size()-1; t1 > 0; t1--)
{
// cout << index[t1]<<endl;
words.erase(words.begin() +( index[t1]-counter));
counter = counter + 1;
}
ASKER
Hello Tommy, Thank you for your answers as usual.
Tommy I tried this but it didn't work i'll tell you why.
Suppose that i have the vector<int> INDEX = {5,6,7}
and I have the vector<int> Letters = {A,B,C,D,E,F,G,H,I,J,K,L}
I want to remove the letters at index 5, 6 and 7 right?
So When i remove the letter at Index 5 which is "F"
Letter = {A,B,C,D,E,G,H,I,J,K,L} And in vector INDEX: 6 should become 5 and 7 should become 6.
What do you think? I'll post this code and let me know if it's right or if my logic is wrong.
Tommy I tried this but it didn't work i'll tell you why.
Suppose that i have the vector<int> INDEX = {5,6,7}
and I have the vector<int> Letters = {A,B,C,D,E,F,G,H,I,J,K,L}
I want to remove the letters at index 5, 6 and 7 right?
So When i remove the letter at Index 5 which is "F"
Letter = {A,B,C,D,E,G,H,I,J,K,L} And in vector INDEX: 6 should become 5 and 7 should become 6.
What do you think? I'll post this code and let me know if it's right or if my logic is wrong.
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] <<" ";
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yes, you were right I tried the one that you suggested it worked perfectly yes.
It's more simple.
Thanks.
It's more simple.
Thanks.
Either going through the array backwards OR subtracting the counter would have worked, but when you do both, you run off the front of the array.
Going through the array backwards is the most standard way to do it. So just remove the 'counter' variable and it should work.