parallelcountif.cpp

The following code example is taken from the book
C++17 - The Complete Guide by Nicolai M. Josuttis, Leanpub, 2017
The code is licensed under a Creative Commons Attribution 4.0 International License. Creative Commons License


#include <vector>
#include <iostream>
#include <algorithm>
#include <execution>  // for the execution policy

int main()
{
  std::vector<int> coll{0, 8, 15, 2, 3, 7, 42};
  //...
  // count elements with even value:
  auto num = std::count_if(std::execution::par,         // execution policy
                           coll.cbegin(), coll.cend(),  // range
                           [](int elem){                // criterion
                             return elem % 2==0;
                           });
  std::cout << "number of elements with even value: " << num << '\n';
}