mercredi 20 juillet 2016

Range for loop with multiple containers

Suppose I have 2 (or more) containers I want to iterate through simultaneously - for example, to compute the dot product of two vectors:

std::vector<double> vector1;
std::vector<double> vector2;    // identical size to vector1

What is the preferred C++11 way to specify a range-for loop over both (or all) containers simultaneously? Is there any reason the syntax in future could not be extended to support this... which seems really readable:

double dotProduct( 0.0 );
for ( auto const & value1 : vector1, auto const & value2 : vector2 )  // illegal!
{
    dotProduct += value1*value2;
}

Aucun commentaire:

Enregistrer un commentaire