1. 程式人生 > >C++ STL 迭代器刪除問題總結

C++ STL 迭代器刪除問題總結

map<int,int>::iterator iter;
for(iter = testMap.begin(); iter != testMap.end(); iter++)
{
    printf("%d " , iter->second );
    if( iter->second == 3 )
    {
        testMap.erase( iter->first ); //注意這裡,是通過key刪除
    }
}