使用stl库c ++创建图形,代码工作正常,但是我在理解代码时遇到麻烦,请帮帮我

问题描述 投票:0回答:1

我对此代码下面的一行有疑问。

-> cout<<"("<<c<<","<<(*i).get_vertex()<<") value of this pair : "<<(*i).get_weight()<<", ";

此get_vertex和get_weight如何在没有类边缘对象帮助的情况下工作。请通过这条线帮助我。代码已成功编译,并且也可以正常工作,但我不知道上面的行是如何工作的。

代码的输出是:

 Pairs for 0 are -> (0,1) value of this pair : 2, (0,2) value of this pair : 3, (0,3) value of this pair : 4,
 Pairs for 1 are -> (1,2) value of this pair : 5, (1,0) value of this pair : 2,
 Pairs for 2 are -> (2,3) value of this pair : 8, (2,1) value of this pair : 5,
 Pairs for 3 are -> (3,0) value of this pair : 4, (3,2) value of this pair : 8,

下面给出代码。

包括

using namespace std;

class edge{

  int weight,vertex;
  public:
    edge(int w , int v){
        weight = w;
        vertex = v;
    }

    int get_weight()const{
        return weight;
    }

    int get_vertex()const{
        return vertex;
    }


};


int main(){

    int n = 4;
    int c = 0;

    vector<list<edge>>adj(n) ;

    adj[0].push_back(edge(2,1));
    adj[0].push_back(edge(3,2));
    adj[0].push_back(edge(4,3));


    adj[1].push_back(edge(5,2));
    adj[1].push_back(edge(2,0));


    adj[2].push_back(edge(8,3));
    adj[2].push_back(edge(5,1));

    adj[3].push_back(edge(4,0));
    adj[3].push_back(edge(8,2));


    vector<list<edge>>:: iterator it ;

    for(it=adj.begin();it!=adj.end();it++){

        cout<<" Pairs for "<<c<<" are -> ";
        list<edge>li = *it;
        list<edge>::iterator i;
        for(i=li.begin();i!=li.end();i++){

            cout<<"("<<c<<","<<(*i).get_vertex()<<") value of this pair : "<<(*i).get_weight()<<", ";
        }
        cout<<endl;
        c++;
    }


}
c++ pointers graph stl
1个回答
0
投票

将它分成大块。添加空格:

cout << "(" 
     << c 
     << "," 
     << (*i).get_vertex() 
     << ") value of this pair : "
     << (*i).get_weight()
     << ", ";

更有意义吗?

© www.soinside.com 2019 - 2024. All rights reserved.