Iterator question

closed account (10oTURfi)
I noticed that whenever I call this function
1
2
3
4
5
6
for(iter=my_list.begin();iter!=my_list.end(); iter++)
{
//alot of code here
if(isTrue)
addsomething(iter);
}

1
2
3
4
5
void addsomething(vector<bunny>::iterator iter)
{
//alot of code here
my_list.push_back(something);
}

iter becomes invalid, which will cause problems to for loop (read: program will crash)

Is there a way to prevent this problem?
Last edited on
If you used list instead of vector it should be fine. Also, you could make it work with iter = insert(iter, something);. Also, you could simply use int index instead of an iterator.
Topic archived. No new replies allowed.