Iterator question
Oct 31, 2011 at 11:54am UTC
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 Oct 31, 2011 at 6:03pm UTC
Oct 31, 2011 at 12:04pm UTC
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.