Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用stl库创建图形是如何工作的?_C++_Pointers_Graph_Stl - Fatal编程技术网

C++ 使用stl库创建图形是如何工作的?

C++ 使用stl库创建图形是如何工作的?,c++,pointers,graph,stl,C++,Pointers,Graph,Stl,我对以下代码行有疑问: cout把它分成几块。 添加空白: cout << "(" << c << "," << (*i).get_vertex() << ") value of this pair : " << (*i).get_weight() << ", "; 我对它是如何编写的没有问题吗?我只想知道(*i)是如何访问类边缘成员函数的。i

我对以下代码行有疑问:

cout把它分成几块。
添加空白:

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

我对它是如何编写的没有问题吗?我只想知道(*i)是如何访问类边缘成员函数的。
i
是一个迭代器;它穿过列表中的边
li
*i
i
指向“Ty”的边,我得到了答案。i是一个迭代器,它就像指向存储在集合中的任何对象的指针,当你取消引用它时,你就得到了对象的一个句柄,在你的例子中,你有一个边列表,所以gb(*i)是对列表中边对象的一个引用
#include<bits/stdc++.h>
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++;
    }


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