注册 登录
编程论坛 C++教室

map中的const-interator

humy 发布于 2012-08-14 19:03, 943 次点击
#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?
只有本站会员才能查看附件,请 登录
只有本站会员才能查看附件,请 登录
5 回复
#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;
}

#3
humy2012-08-17 10:17
回复 2楼 pangding
额。。。不好意思,我刚刚也试了一下,不加const竟然没问题了。
不知道怎么回事。。。
还想请问版主,你在最后注释写的      最好。。空格。。。    这一句建议是什么意思啊?
谢谢
#4
pangding2012-08-17 14:50
C++ 里字符串是用双引号,如 "abc", "    "。单引号用于单个字符,如 'a', 'b', '\n'。
你那里有数个空格,最好用双引号。如果觉得用一个空格隔开也行,那么可以用 ' '。不过即使如此,一般也是用 " " 的多。
#5
humy2012-08-20 20:10
回复 4楼 pangding
奥,谢谢。其实我那里用的是tab
#6
pangding2012-08-20 21:49
回复 5楼 humy
哦,看来是复制在论坛上会自动转成数个空格。
如果是 tab 的话,下回写成 '\t' 好一点。至少不容易造成视觉上的混淆(有时这很严重)。
1