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()
<< ", ";