c++输入流的问题,请教下
#include <iostream>
#include <vector>
using namespace std;
int main(void)
{
vector<int> ivec;
vector<int> ilist;
int val;
cout<<"请输入vector元素"<<endl;
while(cin >> val)
{
ivec.push_back(val);
}
cout<<"请输入list元素"<<endl;
while(cin >> val)
{
ilist.push_back(val);
}
vector<int>::iterator iter1 = ivec.begin();
vector<int>::iterator iter2 = ilist.begin();
while((iter1 != ivec.end()) && (iter2 != ilist.end()))
{
if(*iter1 != *iter2)
break;
++iter1;
++iter2;
}
if((iter1 == ivec.end()) && (iter2 == ilist.end()))
{
cout<<"vector and list have the same emle"<<endl;
}
else
{
cout<<"vector and list different"<<endl;
}
return 0;
}
while(cin >> val
[解决办法]
cin.fail())
{
if(cin.fail())
{
cin.clear(); //清除std::cin的错误状态
cin.sync(); //清空输入缓冲区
break;
}
ivec.push_back(val);
}