1
What is the best way to remove an element from a list by the value of its attribute, without knowing exactly its position?
I realized that the method Erase remove by position, and tried to use along with method find, but I couldn’t understand how to manipulate the return, nor how to pass the parameters correctly.
An example of object structure:
class Tipo{
public:
int id; // Esse valor é único para cada objeto
float valor;
}
My attempt:
void remove_by_id(list<Tipo>& lsita, int id){
// Essa linha não funciona pois o método find deveria receber o objeto e não o atributo (eu acho)
lista.erase( find(lista.begin(), lista.end(), id) );
}
list<Tipo> minha_lista = {...}; // Lista com n elementos
remove_by_id(minha_lista, 3); // Remover o objeto que tem id == 3
Possible, but perhaps using a linked list instead of a vector. https://answall.com/q/163424/101
– Maniero
Okay, I’ll modify the question, because I wouldn’t know how to do it on a list either.
– G. Bittencourt