Containers in STL are divided into two types by storage method: containers stored in arrays (such as vector and deque), and containers stored in discontinuous nodes (such: list, set, map ). When using the erase method to delete elements, pay attention to some issues.
When using list, set, or map traversal to delete some elements, you can use the following method:
correct method 1
STD: List list;
STD: List :: iterator itlist;
for (itlist = List. begin (); itlist! = List. end ();)
{< br> If (willdelete (* itlist)
{< br> itlist = List. erase (itlist);
}< br> else
itlist ++;
}< br>
when using vector and deque to traverse and delete elements, you can also use the erase return value to obtain the location of the next element:
correct usage
STD: vector VEC;
STD: vector :: iterator itvec;
for (itvec = Vec. begin (); itvec! = Vec. end ();)
{< br> If (willdelete (* itvec)
{< br> itvec = Vec. erase (itvec);
}< br> else
itlist ++;
}