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? Does it involve choosing one container/iterator to write short-hand (i.e. for ( auto i : c )
) in a range-for loop, while all other containers/iterators have to be handled long-hand? Is there any reason the syntax in future could not be extended to support short-hand for both/all containers as shown below... which seems really readable:
double dotProduct( 0.0 );
for ( auto const & value1 : vector1, auto const & value2 : vector2 ) // illegal!
{
dotProduct += value1*value2;
}