c++ - How to process several arrays in one range for? -
this question has answer here:
i want copy array using range for
. possible?
something (obviously not working)
unsigned arr[15] = {}; unsigned arr2[15]; (auto : arr, auto &j : arr2) j = i;
or there other tricks avoid operating size of arrays, if know sure of same lenght?
upd @paveldavydov solution. please offer a standard lib solution. c++11 contains pairs , tuples too.
for (auto pair : std::make_tuple(&arr, &arr2));
#include <boost/range/combine.hpp> (const auto& pair : boost::combine(arr, arr2)) { cout << get<0>(pair) << endl; }
update: ok, if want without boost, can implement higher order function that.
template <class t, unsigned long firstlen, unsigned long secondlen, class pred> typename std::enable_if<firstlen == secondlen, void>::type loop_two(t (&first)[firstlen], t (&second)[secondlen], pred pred) { (unsigned long len = 0; len < firstlen; ++len) { pred(first[len], second[len]); } }
and use this:
loop_two(arr, arr1, [] (unsigned a, unsigned b) { cout << << endl; });
Comments
Post a Comment