Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove elements of a vector inside the loop

Tags:

c++

vector

erase

I know that there are similar questions to this one, but I didn’t manage to find the way on my code by their aid. I want merely to delete/remove an element of a vector by checking an attribute of this element inside a loop. How can I do that? I tried the following code but I receive the vague message of error:

'operator =' function is unavailable in 'Player’.

 for (vector<Player>::iterator it = allPlayers.begin(); it != allPlayers.end(); it++)  {      if(it->getpMoney()<=0)           it = allPlayers.erase(it);      else           ++it;  } 

What should I do?

Update: Do you think that the question vector::erase with pointer member pertains to the same problem? Do I need hence an assignment operator? Why?

like image 318
arjacsoh Avatar asked Dec 25 '11 09:12

arjacsoh


People also ask

How do you remove an element from a vector in a loop?

To delete single element from a vector using erase() function, pass the iterator of element to it like erase(it). It will delete the element pointed by the iterator the it variable. To delete multiple elements from a vector using erase() function, pass the iterator range to it like erase(start, end-1).

How do I remove a specific value from a vector?

The erase() function can remove an element from the beginning, within, or end of the vector. In order to remove all the elements from the vector, using erase(), the erase() function has to be repeated the number of times there are elements, beginning from the first element.


1 Answers

You should not increment it in the for loop:

for (vector<Player>::iterator it=allPlayers.begin();                                it!=allPlayers.end();                                /*it++*/) <----------- I commented it. {     if(it->getpMoney()<=0)        it = allPlayers.erase(it);   else        ++it;  } 

Notice the commented part;it++ is not needed there, as it is getting incremented in the for-body itself.

As for the error "'operator =' function is unavailable in 'Player’", it comes from the usage of erase() which internally uses operator= to move elements in the vector. In order to use erase(), the objects of class Player must be assignable, which means you need to implement operator= for Player class.

Anyway, you should avoid raw loop1 as much as possible and should prefer to use algorithms instead. In this case, the popular Erase-Remove Idiom can simplify what you're doing.

allPlayers.erase(     std::remove_if(         allPlayers.begin(),          allPlayers.end(),         [](Player const & p) { return p.getpMoney() <= 0; }     ),      allPlayers.end() );  

1. It's one of the best talks by Sean Parent that I've ever watched.

like image 96
Nawaz Avatar answered Sep 28 '22 09:09

Nawaz