map中的const-interator
#include<string>//#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?