我尝试创建一个空列表数组,然后从main()和Graph()构造函数中打印这些列表的位置。但他们不匹配!请帮忙!
#include<iostream>
#include<list> // use list in STL
using namespace std;
struct Edge {
int v, w;
Edge(int v = -1, int w = 0) : v(v), w(w){}
};
class Graph {
private:
const int V; //number of vertices
int E; //number of edges
list<Edge>* adj[];//adjacency list
public:
Graph(int N):V(N){ //create a graph with no edges
E = 0; list<Edge>* adj[V];
for(int i = 0; i < N; i++){
adj[i] = new list<Edge>();
cout << "at " << i <<" address "<< adj[i] <<"\n";
}
}
friend ostream& operator<<(ostream& out, const Graph& G);
};
ostream& operator<<(ostream& out, const Graph& G){
out << "Vertices: "<< G.V <<" and Edges: "<< G.E <<"\n" << "The adjacency list:\n";
for(int i = 0; i < G.V; i++){
out << "vertex "<< i << ":"<< G.adj[i] <<" -> zero\n";
}
return out;
}
int main() {
Graph* G = new Graph(50);
cout << *G;
}
答案 0 :(得分:-2)
Johnny Mopp,flatmouse
感谢。
更改为以下代码。它奏效了。
Graph(int N):V(N){
E = 0;
for(int i = 0; i < N; i++){
adj[i] = new list<Edge>();
}
}
~Graph(){
for(int i = 0; i < V; i++)
delete adj[i];
}
示例输出:
在0地址0xec3c40
在1地址0xec4070
在2地址0xec4090
在3地址0xec40b0
//
顶点0:0xec3c40 - &gt;零
顶点1:0xec4070 - &gt;零
顶点2:0xec4090 - &gt;零
顶点3:0xec40b0 - &gt;零