2016-12-10 45 views
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这样的文件。

+0

大多数库供应商还没有实现许多C++ 17功能。你不得不等待太久。 – DeiDei

+1

当你在等待libstdC++赶上时,已经有[HPX](http://stellar-group.org/libraries/hpx/)。 – ildjarn

+1

@ildjarn这就是我在编写该示例时使用的内容(然后发布它应该*在* C++中查看的方式...现在编辑以匹配当前草稿) – Cubbi

回答

2

libstdC++(默认使用g ++)或libC++(默认使用clang)都没有实现execution_policy支持。

this page上跟踪在libstdC++中实现新功能的进度。跟踪libC++进度here。正如你所看到的,P0024R2“并行TS应该标准化”至今还没有被任何一家图书馆实施。