vector的遍历删除

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int main()
 4 {
 5     vector<int>num;
 6     for(int i = 0; i < 10; i++)
 7     {
 8         num.push_back(i);
 9     }
10 
11     for(vector<int>::iterator it = num.begin(); it != num.end();)
12     {
13         if(*it % 2 == 0)
14         {
15             it = num.erase(it);
16         }
17         else
18         {
19             it++;
20         }
21     }
22 
23     for (int tmp : num)
24     {
25         cout << tmp << " ";
26     }
27     cout << endl;
28 
29     return 0;
30 }

 

posted @ 2018-03-30 16:20  陈辻柒  阅读(531)  评论(0编辑  收藏  举报