C++ vector, what does this code mean?

前端 未结 6 1134
谎友^
谎友^ 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:07

    1) int nodes = maxnodes, src, dest;

    Here, nodes is an int initialized with the value same as that of maxnodes. src and dest are also int, but with no initial value.

    2) vector g[maxnodes];

    g is here an array of std::vector.

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

    Q. What is g[u][j]?

    A. It is the Edge stored in g at the uth row and jth column.

    Q. g is vector filled with Edge struct, how can it be act like a array of arrays?

    A. Because std::vector has operator[] overloaded for itself in its definition. Refer: http://en.cppreference.com/w/cpp/container/vector/operator_at

提交回复
热议问题