| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 943 人关注过本帖
标题:map中的const-interator
取消只看楼主 加入收藏
humy
Rank: 2
等 级:论坛游民
帖 子:69
专家分:18
注 册:2012-7-23
结帖率:92.86%
收藏
已结贴  问题点数:20 回复次数:2 
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?
图片附件: 游客没有浏览图片的权限,请 登录注册
图片附件: 游客没有浏览图片的权限,请 登录注册
搜索更多相关主题的帖子: family include children 
2012-08-14 19:03
humy
Rank: 2
等 级:论坛游民
帖 子:69
专家分:18
注 册:2012-7-23
收藏
得分:0 
回复 2楼 pangding
额。。。不好意思,我刚刚也试了一下,不加const竟然没问题了。
不知道怎么回事。。。
还想请问版主,你在最后注释写的      最好。。空格。。。    这一句建议是什么意思啊?
谢谢
2012-08-17 10:17
humy
Rank: 2
等 级:论坛游民
帖 子:69
专家分:18
注 册:2012-7-23
收藏
得分:0 
回复 4楼 pangding
奥,谢谢。其实我那里用的是tab
2012-08-20 20:10
快速回复:map中的const-interator
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021053 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved