Reputation: 29
my code is:
class Room {
public:
int id;
string name;
int ownerFd;
Room(int id, string name, int ownerFd)
{
this->id = id;
this->name = name;
this->ownerFd = ownerFd;
}
};
void RemoveUserRooms(int ownerFd) {
for(auto& room : rooms) {
if (room.ownerFd == ownerFd) {
//remove room from list
}
}
}
What I want to do is to remove object from list. I already tried with remove
and erase
but that seems not to work in this way. Is is possible to do with list
?
Upvotes: 0
Views: 770
Reputation: 7542
Use iterator
and erase
while properly updating the iterator.
for(auto i=rooms.begin();i!=rooms.end();)
{
if((*i).ownerFd == ownerFd)
i=rooms.erase(i);
else
i++;
}
Or better ,
you can use remove_if
rooms.remove_if([ownerFd](Room i){return i.ownerFd == ownerFd;});
Upvotes: 2