Help with (const vectorint & t: flights), in floyd algorithm

2
//vector<vector<int>>& flights
//vector<vector<int>> vec(n, vector<int>(k + 1));
for (int i = 1; i <= k; i++)
{
    for (int j = 0; j < n; j++)
    {
        vec[j][i] = vec[j][i - 1];

        for (const vector<int>& t : flights)
        {
            vec[t[1]][i] = min(vec[t[1]][i], vec[t[0]][i - 1] + t[2]);
        }
    }

}

I saw this implementation of the floyd algorithm for the shortest paths but I do not understand what is the 't' that is created and what it contains

    
asked by Daniel 12.11.2018 в 23:43
source

2 answers

2
for (const vector<int>& t : flights)

The for that you see here is an element that was incorporated into the standard C ++ 11 (dating from 2011). It is a loop based on ranges. A possible equivalent to the old way would be:

std::vector<std::vector<int>>::const_iterator it;
for( it = flights.begin(); it != flights.end(); ++it )
{
  std::vector<int> const& t = *it;

  // ...
}

As you can see, t is, in this case, a constant reference to a vector of type int . The reference is used to not make vector copies in each iteration. Going around copying a vector (or a set of vectors) intensively in an algorithm is a very simple and quick way to end your performance.

On the other hand, the reference becomes constant because the algorithm should not modify the state of the collection, it should only be used in read only mode.

    
answered by 13.11.2018 в 07:44
2
  

I do not understand what the 't' is that is created.

The definition of t is:

const vector<int>& t
^^^^^ ^^^^^^ ^^^ ^
  \     \    \    \______ Referencia
   \     \    \_______________________________________ Enteros
    \     \____________________________________ Vector
     \_____________________________ Constante

t is a constant reference to integer vector. It is defined within a for range:

for (const vector<int>& t : flights)
     ^^^^^^^^^^^^^^^^^^^^ ^ ^^^^^^^
              \            \    \___ flights (que es vector<vector<int>>)
               \            \____ en
                \____ Para cada t (en forma de const vector<int>&)

The for of rank makes us an operation for each element of a container, in the previous case it will give a return for each t (which is constant reference to vector of integers) in flights (which is a vector vectors of integers).

Since flights is a vector of integer vectors, each element of flights will be an integer vector; we ask the loop% range co_de that traverses each element of for without making copies (that's why we ask for reference ( flights )) and without modifying the data (that's why we ask that it be constant ( & )).

You could save a few clicks if you let the compiler write the data type for you:

for (const auto& t : flights)
     ^^^^^^^^^^^ ^ ^^^^^^^
              \   \    \___ flights (que es vector<vector<int>>)
               \   \____ en
                \____ Para cada t (referencia constante a lo que toque)
  

And what it contains.

Each const points (as a constant reference) to an element of t , that being a vector of vectors of integers, each flights will be an integer vector.

    
answered by 13.11.2018 в 08:20