C++ vector, what does this code mean?

前端 未结 6 1139
谎友^
谎友^ 2021-01-23 22:27

I have this code:

 const int maxnodes = 5000;
 struct Edge 
 {
   int to, rev;
   int f, cap;
 };

 vector g[maxnodes];

This is qui

6条回答
  •  花落未央
    2021-01-23 23:18

    This

    int nodes = maxnodes, src, dest;
    

    is a declaration that is equivalent to these three declarations

    int nodes = maxnodes;
    int src;
    int dest;
    

    This

    vector g[maxnodes];
    

    is a declaration of an array of objects of type std::vector. You can use the subscript operator with arrays.

    So expression g[u] yields the element of the array with index u that is a reference to an object of type std::vector.

    Class std::vector also overloads the subscript operator. So expression g[u][j] gives the object of type Edge with index j in the vector of the array with index u.

    Dcelaration

    Edge &e = g[u][j];
    

    declares a reference to this object of type Edge.

提交回复
热议问题