C++基础问题 list容器的erase()函数相关
#include<iostream>#include<vector>#include<list>#include<deque&g...
#include <iostream>#include <vector>#include <list>#include <deque>#include <string>using namespace std; /* Exercise Using the following definition of ia, copy ia into a vector and into a list. Use the single 9.26: iterator form of erase to remove the elements with odd values from your list and the even values from your vector. 假设有如下ia 的定义,将ia 复制到一个vector 容器和一个list 容器中。使用单个迭代器参数版本的erase 函数将list 容器中的奇数值元素删除掉,然后将vector 容器中的偶数值元素删除掉。 int ia[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 55, 89 }; */ int main(){ int ia[] = { 0, 1, 1, 2, 3, 5, 8,8,8, 13, 21, 55, 89,8}; size_t len = sizeof(ia)/sizeof(*ia); vector<int> ivect(ia,ia+len); list<int> ilist(ia,ia+len); for(list<int>::iterator iter = ilist.begin();iter!=ilist.end();) { if(*iter%2!=0){ ilist.erase(iter); //程序运行到这里会出错 //ilist.pop_front(); //if(iter==ilist.end()) // break; } else if ( iter!=ilist.end()){ cout << *iter <<"-->belongs to list<int> " << endl; ++iter; } } for(vector<int>::iterator iter = ivect.begin();iter!=ivect.end();) { if(*iter%2==0 ){ ivect.erase(iter); //if(iter==ivect.end()) // break; } else if (iter!=ivect.end()){ cout << *iter <<"-->belongs to vector<int> " << endl; ++iter; } } return 0;} 那位高手能帮我看下这个问题如何解决?谢谢
展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询