?
Solved

runtime Error Erase element vector C++

Posted on 2013-06-14
4
Medium Priority
?
560 Views
Last Modified: 2013-06-14
Hello,
I have a
vector<int>Index = [1,3,4,5,8,9]

Open in new window

vector<int>numbers = [1,2,3,4,5,6,7,8,9,10]

Open in new window


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])
}

Open in new window

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()+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
 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;
   }

Open in new window

0
Comment
Question by:HaniDaher
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 39247275
You had the right idea, you just solved it both ways.
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.
0
 

Author Comment

by:HaniDaher
ID: 39247304
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.

    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] <<"   ";
    }

Open in new window

0
 
LVL 37

Accepted Solution

by:
TommySzalapski earned 2000 total points
ID: 39247372
Yes, but if you do it like this:
for(int t1 = index.size()-1; t1 >= 0; t1--)
   {
       words.erase(words.begin() +( index[t1]));
   }

Open in new window


Then it should work. Because when you erase the element 7, nothing else moves. As long as you know index[] is sorted it works.

Note that this is almost exactly what you tried but without the extra counter. The only difference is you need to use >=0 instead of >0 in the for loop or you miss the first one.
0
 

Author Comment

by:HaniDaher
ID: 39247394
Yes, you were right I tried the one that you suggested it worked perfectly yes.
It's more simple.

Thanks.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Okay. So what exactly is the problem here? How often have we come across situations where we need to know if two strings are 'similar' but not necessarily the same? I have, plenty of times. Until recently, I thought any functionality like that wo…
Iteration: Iteration is repetition of a process. A student who goes to school repeats the process of going to school everyday until graduation. We go to grocery store at least once or twice a month to buy products. We repeat this process every mont…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question