C++ 在结构列表的向量中打印结构的成员

C++ 在结构列表的向量中打印结构的成员,c++,list,vector,struct,C++,List,Vector,Struct,我有一个结构;看起来是这样的: struct Edge { //Represents an edge int v1, v2; // From vertex, To vertex double weight; //

我有一个结构;看起来是这样的:

struct Edge {                                                   //Represents an edge
    int v1, v2;                                             // From vertex, To vertex
    double weight;                                          // Edge weight
    Edge(int vertex1, int vertex2, double wt) :             // Edge constructor
        v1{ vertex1 }, v2{ vertex2 }, weight{ wt } {};        // from, to, weight
};
vector<list<Edge>> adjacent;
我有这些结构列表的向量;看起来是这样的:

struct Edge {                                                   //Represents an edge
    int v1, v2;                                             // From vertex, To vertex
    double weight;                                          // Edge weight
    Edge(int vertex1, int vertex2, double wt) :             // Edge constructor
        v1{ vertex1 }, v2{ vertex2 }, weight{ wt } {};        // from, to, weight
};
vector<list<Edge>> adjacent;
向量邻接;
我已经初始化了列表向量,但我不知道如何打印所有边的权重成员

list<Edge> ::iterator gr;               //list iterator
    for (int x = 0; x < numVertices; x++) {
        for (gr = adjacent[x].begin(); gr != adjacent[x].end(); ++gr) {
            cout << *gr.weight;
        }
    }
list::迭代器gr//列表迭代器
对于(int x=0;x
cout << gr->weight;
cout权重;
也相当于做:

cout << (*gr).weight;

cout
*gr.weight
不首先遵从
迭代器。它等价于
*(gr.weight)


将其更改为
(*gr).weight
gr->weight;
gr
是一个迭代器,在许多方面它的行为类似于一个指针,因此您应该使用
gr->weight
而不是
*gr.weight


对于
循环,请考虑基于范围的

for (const auto &edgeList : adjacent)
{
    for (const auto &edge : edgeList)
    {
        cout << edge.weight << ' ';
    }
    cout << endl;
}
for(常数自动和边列表:相邻)
{
用于(常量自动和边:边列表)
{

这行得通。谢谢。只是想知道,如果我取消引用它,为什么它不起作用?@OldIslander对此的答案与此有关。
*a
(间接)运算符的优先级低于
(成员访问权限)在您的例子中,它试图找到名为
weight
的迭代器的成员,但失败了。