Refreshing the iterator
-
Hi All, To demonstrate my problem,Have put in a small piece of code below : Iam putting some strings into my list & while iterating the list,want to remove a particular element from the list.The following code crashes when I iterate further. I know that I have to refresh my iterator somehow but how?. How could I get around this problem? Thanks.. list pool; pool.push_back("PageType1"); pool.push_back("PageType2"); pool.push_back("PageType3"); pool.push_back("PageType4"); list::iterator iter = pool.begin(); while(iter!=pool.end()) { long size = pool.size(); if(*iter=="PageType2") pool.remove("PageType2"); iter++; }
-
Hi All, To demonstrate my problem,Have put in a small piece of code below : Iam putting some strings into my list & while iterating the list,want to remove a particular element from the list.The following code crashes when I iterate further. I know that I have to refresh my iterator somehow but how?. How could I get around this problem? Thanks.. list pool; pool.push_back("PageType1"); pool.push_back("PageType2"); pool.push_back("PageType3"); pool.push_back("PageType4"); list::iterator iter = pool.begin(); while(iter!=pool.end()) { long size = pool.size(); if(*iter=="PageType2") pool.remove("PageType2"); iter++; }
What you need to do is build a list of items to remove, then remove them all in another loop. You can't remove items from a list, because that item contains the pointer to the next item. Christian Graus - Microsoft MVP - C++
-
What you need to do is build a list of items to remove, then remove them all in another loop. You can't remove items from a list, because that item contains the pointer to the next item. Christian Graus - Microsoft MVP - C++