1
有人可以告诉我如何编写std::reduce
的示例代码,它位于cppreference?如何使用g ++ 6编译std :: reduce和#include <execution_policy>的代码?
#include <iostream>
#include <chrono>
#include <vector>
#include <numeric>
#include <execution_policy>
int main()
{
std::vector<double> v(10'000'007, 0.5);
{
auto t1 = std::chrono::high_resolution_clock::now();
double result = std::accumulate(v.begin(), v.end(), 0.0);
auto t2 = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::milli> ms = t2 - t1;
std::cout << std::fixed << "std::accumulate result " << result
<< " took " << ms.count() << " ms\n";
}
{
auto t1 = std::chrono::high_resolution_clock::now();
double result = std::reduce(std::par, v.begin(), v.end());
auto t2 = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::milli> ms = t2 - t1;
std::cout << "std::reduce result "
<< result << " took " << ms.count() << " ms\n";
}
}
我在Mint18下安装了新鲜的g ++ 6.2。在编译期间,我只使用-std=c++17
标志。如果我想使用execution_policy
(#include <execution_policy>
),该怎么办?目前我的编译器告诉我,reduce
不是名称空间std
的成员,并且没有像execution_policy
这样的文件。
大多数库供应商还没有实现许多C++ 17功能。你不得不等待太久。 – DeiDei
当你在等待libstdC++赶上时,已经有[HPX](http://stellar-group.org/libraries/hpx/)。 – ildjarn
@ildjarn这就是我在编写该示例时使用的内容(然后发布它应该*在* C++中查看的方式...现在编辑以匹配当前草稿) – Cubbi