关于I/O的问题

jianfengshxy 2008-03-13 01:31:45
这个程序是用来比较list和vect的数据是否完全相等.
问题在于输入vect数据时无法结束本此输入,如果按ctrl+d就结束程序了,无法输入list数据.
以前遇到while(cin>>ival)时我都是按ctrl+d结束输入的,后面的程序正常进行,不知道这次为什么不形?
(in linux)

#include<iostream>
#include<list>
#include<vector>
using namespace std;

int main()
{
vector<int> ivec;
list<int> ilst;
int ival;

cout<<"the vetor's numbers"<<endl;
while(cin>>ival)
ivec.push_back(ival);

cout<<"the list's numbers"<<endl;
while(cin>>ival)
ivec.push_back(ival);

cin.clear();

vector<int>::const_iterator vecit=ivec.begin();
list <int>::const_iterator lstit=ilst.begin();

if(ilst.size()!=ivec.size())
{
cout<<"they are not same"<<endl;
return 0;
}
else
{ for(;vecit!=ivec.end(),lstit!=ilst.end();++vecit,++lstit)
{
if(*vecit!=*lstit)
{ cout<<"they are not same"<<endl;
return 0;
}
}
cout<<"they are same!"<<endl;
}
return 0;
}
...全文
160 12 打赏 收藏 举报
写回复
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
i_coding 2008-03-14
  • 打赏
  • 举报
回复
楼上的老兄,貌似只用cin.clear();来重设有效状态是不行的,贴这么的错误代码出来干嘛呢。

只用cin.clear()至少在vs.net 2003是运行错误的吧。
i_coding 2008-03-13
  • 打赏
  • 举报
回复
加了两条代码,这要问题就是cin流的状态和缓冲区的问题

#include <iostream> 
#include <list>
#include <vector>
#include <limits> //<----添加此头文件
using namespace std;

int main(void)
{
vector <int> ivec;
list <int> ilst;
int ival;
cout << "the vetor's numbers" << endl;
while(cin >> ival)
ivec.push_back(ival);
cin.clear(); //<-----这里
cin.ignore(std::numeric_limits<streamsize>::max(),'\n'); // <-----这里
cout << "the list's numbers" << endl;
while(cin >> ival)
ilst.push_back(ival); //<-------这里
vector <int> ::const_iterator vecit = ivec.begin();
list <int> ::const_iterator lstit = ilst.begin();
if(ilst.size() != ivec.size())
{
cout << "they are not same" << endl;
return 0;
}
else
{
for( ; vecit != ivec.end(), lstit != ilst.end(); ++vecit, ++lstit)
{
if(*vecit != *lstit)
{
cout << "they are not same" << endl;
return 0;
}
}
cout << "they are same!" << endl;
}
return 0;
}
Lynn_Ran 2008-03-13
  • 打赏
  • 举报
回复

#include <iostream>
#include <list>
#include <vector>
using namespace std;

int main()
{
vector <int> ivec;
list <int> ilst;
int ival;

cout <<"the vetor's numbers" <<endl;
while(cin>>ival)
ivec.push_back(ival);

cin.clear();//<=========增加这个

cout <<"the list's numbers" <<endl;
while(cin>> ival)
ilst.push_back(ival);//<=========容器写错了

cin.clear();

vector <int> ::const_iterator vecit=ivec.begin();
list <int> ::const_iterator lstit=ilst.begin();

if(ilst.size()!=ivec.size())
{
cout <<"they are not same" <<endl;
return 0;
}
else
{ for(;vecit!=ivec.end(),lstit!=ilst.end();++vecit,++lstit)
{
if(*vecit!=*lstit)
{ cout <<"they are not same" <<endl;
return 0;
}
}
cout <<"they are same!" <<endl;
}
return 0;
}
Lynn_Ran 2008-03-13
  • 打赏
  • 举报
回复

#include <iostream>
#include <list>
#include <vector>
using namespace std;

int main()
{
vector <int> ivec;
list <int> ilst;
int ival;

cout <<"the vetor's numbers" <<endl;
while(cin>>ival)
ivec.push_back(ival);

cin.clear();//<=========增加这个

cout <<"the list's numbers" <<endl;
while(cin>> ival)
ilst.push_back(ival);//<=========容器写错了

cin.clear();

vector <int> ::const_iterator vecit=ivec.begin();
list <int> ::const_iterator lstit=ilst.begin();

if(ilst.size()!=ivec.size())
{
cout <<"they are not same" <<endl;
return 0;
}
else
{ for(;vecit!=ivec.end(),lstit!=ilst.end();++vecit,++lstit)
{
if(*vecit!=*lstit)
{ cout <<"they are not same" <<endl;
return 0;
}
}
cout <<"they are same!" <<endl;
}
return 0;
}
Lynn_Ran 2008-03-13
  • 打赏
  • 举报
回复

#include <iostream>
#include <list>
#include <vector>
using namespace std;

int main()
{
vector <int> ivec;
list <int> ilst;
int ival;

cout <<"the vetor's numbers" <<endl;
while(cin>>ival)
ivec.push_back(ival);

cin.clear();//<=========增加这个

cout <<"the list's numbers" <<endl;
while(cin>> ival)
ilst.push_back(ival);//<=========容器写错了

cin.clear();

vector <int> ::const_iterator vecit=ivec.begin();
list <int> ::const_iterator lstit=ilst.begin();

if(ilst.size()!=ivec.size())
{
cout <<"they are not same" <<endl;
return 0;
}
else
{ for(;vecit!=ivec.end(),lstit!=ilst.end();++vecit,++lstit)
{
if(*vecit!=*lstit)
{ cout <<"they are not same" <<endl;
return 0;
}
}
cout <<"they are same!" <<endl;
}
return 0;
}
Lynn_Ran 2008-03-13
  • 打赏
  • 举报
回复

#include <iostream>
#include <list>
#include <vector>
using namespace std;

int main()
{
vector <int> ivec;
list <int> ilst;
int ival;

cout <<"the vetor's numbers" <<endl;
while(cin>>ival)
ivec.push_back(ival);

cin.clear();//<=========增加这个

cout <<"the list's numbers" <<endl;
while(cin>> ival)
ilst.push_back(ival);//<=========容器写错了

cin.clear();

vector <int> ::const_iterator vecit=ivec.begin();
list <int> ::const_iterator lstit=ilst.begin();

if(ilst.size()!=ivec.size())
{
cout <<"they are not same" <<endl;
return 0;
}
else
{ for(;vecit!=ivec.end(),lstit!=ilst.end();++vecit,++lstit)
{
if(*vecit!=*lstit)
{ cout <<"they are not same" <<endl;
return 0;
}
}
cout <<"they are same!" <<endl;
}
return 0;
}
Lynn_Ran 2008-03-13
  • 打赏
  • 举报
回复

#include <iostream>
#include <list>
#include <vector>
using namespace std;

int main()
{
vector <int> ivec;
list <int> ilst;
int ival;

cout < <"the vetor's numbers" < <endl;
while(cin> > ival)
ivec.push_back(ival);

cin.clear(); //记得把输入流清空下

cout < <"the list's numbers" < <endl;
while(cin> > ival)
ilst.push_back(ival); //这里的ivec.push_back(ival); 写错了

cin.clear();

vector <int> ::const_iterator vecit=ivec.begin();
list <int> ::const_iterator lstit=ilst.begin();

if(ilst.size()!=ivec.size())
{
cout < <"they are not same" < <endl;
return 0;
}
else
{ for(;vecit!=ivec.end(),lstit!=ilst.end();++vecit,++lstit)
{
if(*vecit!=*lstit)
{ cout < <"they are not same" < <endl;
return 0;
}
}
cout < <"they are same!" < <endl;
}
return 0;
}
jianfengshxy 2008-03-13
  • 打赏
  • 举报
回复
谢谢了,第一次输入结束后应该清除状态
jianfengshxy 2008-03-13
  • 打赏
  • 举报
回复
谢谢了,第一次输入结束后应该清除状态
jianfengshxy 2008-03-13
  • 打赏
  • 举报
回复
谢谢了,第一次输入结束后应该清除状态
jianfengshxy 2008-03-13
  • 打赏
  • 举报
回复
谢谢,第一次输入后忘了清除状态了.有了这次教训我想我以后一定记住了.
  • 打赏
  • 举报
回复

#include <iostream>
#include <list>
#include <vector>
using namespace std;

int main()
{
vector <int> ivec;
list <int> ilst;
int ival;

cout <<"the vetor's numbers" <<endl;
while(cin>>ival)
ivec.push_back(ival);

cin.clear();//<=========增加这个

cout <<"the list's numbers" <<endl;
while(cin>> ival)
ivec.push_back(ival);

cin.clear();

vector <int> ::const_iterator vecit=ivec.begin();
list <int> ::const_iterator lstit=ilst.begin();

if(ilst.size()!=ivec.size())
{
cout <<"they are not same" <<endl;
return 0;
}
else
{ for(;vecit!=ivec.end(),lstit!=ilst.end();++vecit,++lstit)
{
if(*vecit!=*lstit)
{ cout <<"they are not same" <<endl;
return 0;
}
}
cout <<"they are same!" <<endl;
}
return 0;
}
相关推荐
发帖
C++ 语言

6.3w+

社区成员

C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
帖子事件
创建了帖子
2008-03-13 01:31
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下