January 2018
Intermediate to advanced
374 pages
9h 53m
English
What we can do instead is to combine std::for_each() with the LinearRange class described in Chapter 5, A Deeper Look at Iterators. To remind you, the LinearRange class is constructed via the function make_linear_range() which returns a range of numbers that can be iterated just like a regular container.
An index-based for-loop based on an STL algorithm can be created like this:
auto first_idx = size_t{0};
auto last_idx = mice.size();
auto indices = make_linear_range(first_idx, last_idx, last_idx);
std::for_each(indices.begin(), indices.end(), [&mice](size_t i){
std::cout << i << " " << mice[i] << '\n';
});
This can then be further parallelized with an execution policy of choice:
auto p = std::execution::par; ...