20:3:注意:初始化'graph :: graph(int)'的参数1

时间:2019-04-15 12:41:57

标签: c++

有人可以解释为什么我遇到这个错误吗?:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

struct edge {
  int from, to;
  ll w; // weight

  edge(int u, int v, ll w = 0) {
    this->from = to;
    this->from = to;
    this->w = w;
  }
};

struct graph {
  int size;
  vector<edge*>* edges;

  graph(int size) {
      this->size = size;
      this->edges = new vector<edge*>[size]();
  }
  ~graph() { delete[] edges; }
  void add(edge* e) { this->edges[e->from].push_back(e); }
};

1 个答案:

答案 0 :(得分:3)

我不确定,因为您没有发布明确给出的错误,但是我认为您的结构是不正确的。您正在制作向量的dynamic array,而数组实际上并不是边缘,而是顶点列表,adjacency list

但是,我建议使用以下struct

struct graph
{
    struct edge_data
    {
        int to_;
        ll wt_;
    };
    vector<vector<edge_data>> g_;
    graph(int sz) : g_(sz) {}
    void add(int from, int to, ll wt)
    {
        g_.at(from).push_back(edge_data(to, wt));
    }
};

如果需要,可以使用edge类型,但不要将其存储在graph中。

  

无论如何都没有newdelete也没有arrays。我们有矢量来处理所有这些事情。