Remove element from STL list
Hello,
please, I need to have a list of objects that are frequently inserted and removed.
That's why I decided to use STL list.
However, I have problems removing elements from the list.
For example this code won't compile:
Code:
#include <iostream>
#include <list>
#include <string>
using namespace std;
struct Klijent
{
string ime;
};
int main()
{
Klijent k;
k.ime="Name";
list<Klijent>lista;
lista.push_back(k);
lista.remove(k);
}
One way to slove this is to use iterators:
Code:
for (it = lista.begin(); it != lista.end(); ++it)
{
if (it->ime == "Name")
{
lista.erase(it);
}
}
but, this gives me an exception. I don't know what is wrong.
I wonder if there is a clever way to do this?
There's been a while since I used STL and I almost forget it.
Thanks in advance.