Parallelizing an index-based for-loop

Even though we recommend using algorithms, sometimes a raw, index-based for-loop is required for a specific task. The STL algorithms do provide an equivalent of a range-based for-loop, but there is no equivalent of a regular index-based for-loop.

In other words, a range based for-loop is equal to the STL algorithm for_each...

auto mice = std::vector<std::string>{"Mickey", "Minnie", "Jerry"}; // Range based for loop
for(auto m: mice) {   std::cout << m << '\n'; }// STL algorithm std::for_eachstd::for_each(mice.begin(), mice.end(), [](auto m){ 
  std::cout << m << '\n'; 
}); 

...but, there is no STL algorithm equivalent of an index-based for-loop:

for(size_t i = 0; i < mice.size(); ++i) { std::cout << i << ...

Get C++ High Performance now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.