tilblackout
2019-06-13 18:16
采纳率: 88%
浏览 375

C++迭代器用法的问题...

class QQ
{
public:
    int id;
    string name;
public:
    QQ(int a,string b):id(a),name(b){}
};
class Group
{
public:
    string Gname;
    vector<QQ*> qlist;
    vector<QQ*>::iterator i;
public:
    Group(string a):Gname(a){}
    void addQQ(const QQ& tmp)
    {
        qlist.push_back(new QQ(tmp.id,tmp.name));
    }
    void print()
    {
        i=qlist.begin();
        cout<<"qq_"<<i->id<<" "<<i->id;
        for(;i!=qlist.end();i++)
        {
            cout<<";qq_"<<i->id<<" "<<i->name;
        }
    }
};

我想通过迭代器访问QQ类的内容,i->id不行访问,那要怎么写呢

  • 点赞
  • 收藏

1条回答 默认 最新

  • blownewbee 2019-06-13 19:23
    已采纳
    class QQ
    {
    public:
        int id;
        string name;
    public:
        QQ(int a,string b):id(a),name(b){}
    };
    class Group
    {
    public:
        string Gname;
        vector<QQ*> qlist;
        vector<QQ*>::iterator i;
    public:
        Group(string a):Gname(a){}
        void addQQ(const QQ& tmp)
        {
            qlist.push_back(new QQ(tmp.id,tmp.name));
        }
        void print()
        {
            i=qlist.begin();
            cout<<"qq_"<<(*i)->id<<" "<<(*i)->id;
            for(;i!=qlist.end();i++)
            {
                cout<<";qq_"<<(*i)->id<<" "<<(*i)->name;
            }
        }
    };
    
    点赞 打赏 评论

相关推荐 更多相似问题