STL中map/vector的删除元素操作

 在我们使用C++中的STL的时候,可以使用迭代器iterator进行遍历,但是当我们通过iterator对vector和map删除元素的时候,要格外的小心,往往操作不当,导致iterator失效,后果就是程序奔溃。

    1. 对于vector,erase会返回下一个iterator。所以一般采用的方法是:

     因为在使用erase的时候,删除元素前面的iterator有效,但是后面的iterator就不可预知了。

[cpp]  view plain  copy
  1. #include <iostream>  
  2. #include <vector>  
  3. #include <iterator>  
  4. using namespace std;  
  5.   
  6. void displayAllDate(vector<int>& inputDate)  
  7. {  
  8.     vector<int>::iterator itor = inputDate.begin();  
  9.     cout << endl;  
  10.     while (itor != inputDate.end())  
  11.     {  
  12.         cout << *itor << " ";  
  13.         ++itor;  
  14.     }  
  15. }  
  16.   
  17. int main()  
  18. {  
  19.     vector<int> inputDate;  
  20.     for (int i = 0; i < 10; i++)  
  21.     {  
  22.         inputDate.push_back(i);  
  23.     }  
  24.       
  25.     vector<int>::iterator iter = inputDate.begin();  
  26.     while (iter != inputDate.end())  
  27.     {  
  28.         if (0 == (*iter)%2)  
  29.         {  
  30.             iter = inputDate.erase(iter);  
  31.         }  
  32.         else  
  33.         {  
  34.             ++iter;  
  35.         }  
  36.     }  
  37.   
  38.     displayAllDate(inputDate);  
  39.     return 0;  
  40. }  
     运行结果:

    

    2. 对于map,删除erase之后,只会影响到当前的iterator,因此我们一般推荐以下方法:

[cpp]  view plain  copy
  1. #include <iostream>  
  2. #include <map>  
  3. #include <iterator>  
  4. using namespace std;  
  5.   
  6. void displayAllDate(map<intint>& inputDate)  
  7. {  
  8.     map<intint>::iterator itor = inputDate.begin();  
  9.     cout << endl;  
  10.     while (itor != inputDate.end())  
  11.     {  
  12.         cout << itor->second << " ";  
  13.         ++itor;  
  14.     }  
  15. }  
  16.   
  17. int main()  
  18. {  
  19.     map<intint> inputDate;  
  20.     for (int i = 0; i < 10; i++)  
  21.     {  
  22.         inputDate[i] = i + 10;  
  23.     }  
  24.   
  25.     map<intint>::iterator iter = inputDate.begin();  
  26.     while (iter != inputDate.end())  
  27.     {  
  28.         if (0 == (iter->second) % 2)  
  29.         {  
  30.             inputDate.erase(iter++);  
  31.         }  
  32.         else  
  33.         {  
  34.             ++iter;  
  35.         }  
  36.     }  
  37.   
  38.     displayAllDate(inputDate);  
  39.     return 0;  
  40. }  
    运行结果:

    

   但是要注意的一点,map的erase操作在window下面也可以用vector的方法实现,但是在linux下是编译不过的。所以推荐使用上面的方法。

[cpp]  view plain  copy
  1. #include <iostream>  
  2. #include <map>  
  3. #include <iterator>  
  4. using namespace std;  
  5.   
  6. void displayAllDate(map<intint>& inputDate)  
  7. {  
  8.     map<intint>::iterator itor = inputDate.begin();  
  9.     cout << endl;  
  10.     while (itor != inputDate.end())  
  11.     {  
  12.         cout << itor->second << " ";  
  13.         ++itor;  
  14.     }  
  15. }  
  16.   
  17. int main()  
  18. {  
  19.     map<intint> inputDate;  
  20.     for (int i = 0; i < 10; i++)  
  21.     {  
  22.         inputDate[i] = i + 10;  
  23.     }  
  24.   
  25.     map<intint>::iterator iter = inputDate.begin();  
  26.     while (iter != inputDate.end())  
  27.     {  
  28.         if (0 == (iter->second) % 2)  
  29.         {  
  30.             iter = inputDate.erase(iter);  
  31.         }  
  32.         else  
  33.         {  
  34.             ++iter;  
  35.         }  
  36.     }  
  37.   
  38.     displayAllDate(inputDate);  
  39.     return 0;  
  40. }  
    运行结果是:

    

    参考:

    http://www.cnblogs.com/dabaopku/p/3912662.html

猜你喜欢

转载自blog.csdn.net/liu0808/article/details/80568559