结构回推不起作用

Structure Push back not working

本文关键字:不起作用 结构      更新时间:2023-10-16
#include<cstdio>
#include<vector>
using namespace std;
struct Edge
{
    int from;
    int to;
    int weight;
};
int main()
{
    vector<Edge> v;
    v.push_back(Edge (1,2,10));
    v.push_back(Edge (2,3,30));
    v.push_back(Edge (1,3,20));
    return 0;
   }

为什么push_back函数在向量中不起作用并给出错误? 如何直接在 vector 中插入值而不定义 Edge 类型的变量?

除了注释中提供的答案外,您还可以简单地在结构中添加构造函数。

Edge(int f, int t, int w) {
    from = f;
    to = t;
    weight = w;
}