1 #include <iostream> 2 #include <vector> 3 #include <list> 4 using namespace std; 5 6 int main() 7 { 8 int ia[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 55, 89 }; 9 vector<int> ivec(ia, ia + 11); 10 list<int> ilist(ia, ia + 11); 11 12 //删除vector中的偶数 13 for (auto iter = ivec.begin(); iter != ivec.end();) 14 { 15 if (*iter % 2 == 0) 16 iter = ivec.erase(iter); 17 else 18 ++iter; 19 } 20 21 //删除list中的奇数 22 for (auto iter = ilist.begin(); iter != ilist.end();) 23 { 24 if (*iter % 2 == 1) 25 { 26 iter = ilist.erase(iter++); 27 } 28 else 29 ++iter; 30 } 31 32 for (auto i : ivec) 33 { 34 cout << i << ‘ ‘; 35 } 36 37 for (auto i : ilist) 38 { 39 cout << i << ‘ ‘; 40 } 41 system("pause"); 42 }
时间: 2024-12-24 21:56:10