这是一个简单的 C++ 图形算法
#include <iostream>
#include <string>
#include <list>
#include <vector>
using namespace std;
class Graph {
public:
Graph(int v = 1):
vertexs(v), edges(0) {
adj = new list<int>[vertexs];
}
void test_put() {
int iter_cnt = 0;
for (int i = 0; i < vertexs; i++) {
for (int j = 0; j < 3; j++) {
adj[i].push_back(iter_cnt++);
}
}
}
void test_print() {
list<int>:: iterator iter;
for (int i = 0; i < vertexs; i++) {
for (iter = adj[i].begin(); iter != adj[i].end(); iter++) {
cout << *iter << "->";
}
cout << "###" << endl;
}
}
private:
int vertexs;
int edges;
list<int> adj[];
};
int main() {
Graph g(10);
g.test_put();
g.test_print();
}
有错误
ian@ubuntu:~/tmp$ g++ wgraph.cpp -o wg
wgraph.cpp: In constructor ‘Graph::Graph(int)’:
wgraph.cpp:12:44: error: incompatible types in assignment of ‘std::list<int>*’ to ‘std::list<int> [0]’
我是一名 java 程序员,我不知道如何在 c++ 中制作正确的构造函数。