would this list be safe?

the code-

1
2
3
4
5
6
7
8
9
10
11
12
Iter1 = mList.begin();

do
{
if(*Iter1 == m_bAge)
{
	//do code
	mList.erase(m_bAge)
}
Iter1++;
}
while(Iter1 != mList.end())


i'm not sure if it would be safe to erase the list while also increasing the value just after erasing one or being in a loop with it while doing that, i don't want to miss a value inside of the list also, so is the above code good for use?
Last edited on
Not really because when you erase an element any iterators pointing to it are invalidated.

I would use something like this:
1
2
3
4
5
6
7
	while(Iter1 != mList.end())
	{
		if(*Iter1 == m_bAge)
			Iter1 = mList.erase(Iter1);
		else
			++Iter1;
	}
Last edited on
Topic archived. No new replies allowed.