![]() |
#2
pangding2012-08-16 12:19
回复 楼主 humy
在我这加不加 const 都正确。只发现了一个小错误,另外把该加的头文件都加上试试。
![]() #include<string> //#include<stringstream> #include <vector> #include <map> #include <iostream> using namespace std; typedef map<string,vector<string> > Familytree; int main () { vector<string>::const_iterator out;////////////////////看这 Familytree::const_iterator it;////////////////////////看这 Familytree family; string fn,cn,line; cout<<"enter family name "<<endl; while(1) { getline(cin,fn); if(fn=="@") break; cout<<"enter children's names ctrl+z to end"<<endl; //getline(cin,line); //istringsstream stream(line); //while(stream>>cn) vector<string> nvec; while(cin>>cn) { cout<<"here"<<endl; //check nvec.push_back(cn); } cin.clear(); //cout<<"fail:"<<cin.fail()<<endl;//check istream //cout<<"end:"<<cin.eof()<<endl; //cout<<"bad:"<<cin.bad()<<endl; family.insert(make_pair(fn,nvec)); cout<<"if you have finished all,enter @;otherwise,enter family name"<<endl; } cout<<"enter the family name you want to check ctrl+z to end"<<endl; while(1) { getline(cin,fn);//cin>>fn; if(fn=="@") break; it=family.find(fn); if(it==family.end()) cout<<"the family doesn't exist in the list"<<endl; else { for(out=it->second.begin();out!=it->second.end();out++) cout<<*out<<' '; // 最好是写 " ",要么就一个空格 ' '。 cout<<endl; } cout<<"if you have finished all,enter @;otherwise,enter family name"<<endl; } return 0; } |
//#include<stringstream>
using namespace std;
typedef map<string,vector<string> > Familytree;
int main ()
{
vector<string>::const_iterator out;////////////////////看这
Familytree::const_iterator it;////////////////////////看这
Familytree family;
string fn,cn,line;
cout<<"enter family name "<<endl;
while(1)
{
getline(cin,fn);
if(fn=="@")
break;
cout<<"enter children's names ctrl+z to end"<<endl;
//getline(cin,line);
//istringsstream stream(line);
//while(stream>>cn)
vector<string> nvec;
while(cin>>cn)
{ cout<<"here"<<endl; //check
nvec.push_back(cn);
}
cin.clear();
//cout<<"fail:"<<cin.fail()<<endl;//check istream
//cout<<"end:"<<cin.eof()<<endl;
//cout<<"bad:"<<cin.bad()<<endl;
family.insert(make_pair(fn,nvec));
cout<<"if you have finished all,enter @;otherwise,enter family name"<<endl;
}
cout<<"enter the family name you want to check ctrl+z to end"<<endl;
while(1)
{
getline(cin,fn);//cin>>fn;
if(fn=="@")
break;
it=family.find(fn);
if(it==family.end())
cout<<"the family doesn't exist in the list"<<endl;
else
{
for(out=it->second.begin();out!=it->second.end();out++)
cout<<*out<<' ';
cout<<endl;
}
cout<<"if you have finished all,enter @;otherwise,enter family name"<<endl;
}
return 0;
}是我写的一个存家谱的小程序。
原来在程序中是Familytree::iterator it;vector<string>::iterator out;/(可练习册上就是这样写的)?就会出错,如图 额。。。。改成const_iterator就对了,可练习册上答案也没写const,他错了?一定要用const?
只有本站会员才能查看附件,请 登录
只有本站会员才能查看附件,请 登录