#include <algorithm> #include <chrono> #include <cstdint> #include <execution> #include <iostream> #include <random> #include <vector> std::random_device rd; std::mt19937_64 mt{rd()}; template <typename T> T gen_random(T min, T max) { std::uniform_int_distribution<T> uid(min, max); return uid(mt); } void vector_sort_execution_par(const int &len) { std::vector<std::uint32_t> vec; for (int i = 0; i < len; i++) { vec.push_back(gen_random<std::uint32_t>(0, UINT32_MAX)); } for (auto i : vec) { std::cout << i << "\t"; } std::cout << std::endl; std::sort(std::execution::par, vec.begin(), vec.end()); std::cout << "After sort parallel:" << std::endl; for (auto i : vec) { std::cout << i << "\t"; } std::cout << std::endl; } void vector_transformer_execution_par() { std::vector<int> vec1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; std::vector<int> vec2(10); std::transform(std::execution::par, vec1.begin(), vec1.end(), vec2.begin(), [](int x) { return x * x; }); for (auto i : vec2) { std::cout << i << "\t"; } std::cout << std::endl; } void vector_execution_par_unseq(const int &len) { std::vector<std::uint32_t> vec; for (int i = 0; i < len; i++) { std::uint32_t temp=gen_random<std::uint32_t>(0, UINT32_MAX); vec.push_back(temp); } std::for_each(std::execution::par_unseq, vec.begin(), vec.end(), [](std::uint32_t x) { std::cout << x << "\t"; }); std::cout << std::endl; } int main(int args, char **argv) { vector_execution_par_unseq(atoi(argv[1])); }
g++-12 -std=c++2a -I. *.cpp -o h1
./h1 10;
标签:std,par,execution,int,vec,include From: https://www.cnblogs.com/Fred1987/p/17506146.html