| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 943 人关注过本帖
标题:map中的const-interator
只看楼主 加入收藏
humy
Rank: 2
等 级:论坛游民
帖 子:69
专家分:18
注 册:2012-7-23
结帖率:92.86%
收藏
已结贴  问题点数:20 回复次数:5 
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
pangding
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:北京
等 级:贵宾
威 望:94
帖 子:6784
专家分:16751
注 册:2008-12-20
收藏
得分:0 
回复 楼主 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;
}

2012-08-16 12:19
humy
Rank: 2
等 级:论坛游民
帖 子:69
专家分:18
注 册:2012-7-23
收藏
得分:0 
回复 2楼 pangding
额。。。不好意思,我刚刚也试了一下,不加const竟然没问题了。
不知道怎么回事。。。
还想请问版主,你在最后注释写的      最好。。空格。。。    这一句建议是什么意思啊?
谢谢
2012-08-17 10:17
pangding
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:北京
等 级:贵宾
威 望:94
帖 子:6784
专家分:16751
注 册:2008-12-20
收藏
得分:20 
C++ 里字符串是用双引号,如 "abc", "    "。单引号用于单个字符,如 'a', 'b', '\n'。
你那里有数个空格,最好用双引号。如果觉得用一个空格隔开也行,那么可以用 ' '。不过即使如此,一般也是用 " " 的多。
2012-08-17 14:50
humy
Rank: 2
等 级:论坛游民
帖 子:69
专家分:18
注 册:2012-7-23
收藏
得分:0 
回复 4楼 pangding
奥,谢谢。其实我那里用的是tab
2012-08-20 20:10
pangding
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:北京
等 级:贵宾
威 望:94
帖 子:6784
专家分:16751
注 册:2008-12-20
收藏
得分:0 
回复 5楼 humy
哦,看来是复制在论坛上会自动转成数个空格。
如果是 tab 的话,下回写成 '\t' 好一点。至少不容易造成视觉上的混淆(有时这很严重)。
2012-08-20 21:49
快速回复:map中的const-interator
数据加载中...
 
   



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

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