微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 C++ 中使用 <execution> 头文件的问题

如何解决在 C++ 中使用 <execution> 头文件的问题

我在 Windows 10 上有 gcc 10.2.0。所以,在这个最新版本的 gcc 中实现了。
问题是当我从以下链接复制示例代码时:https://docs.w3cub.com/cpp/algorithm/reduce,它说:std::execution 尚未声明。有什么想法吗?

#include <iostream>
#include <chrono>
#include <vector>
#include <numeric>
#include <execution>
 
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::execution::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";
    }
}

解决方法

为了向后兼容,许多编译器默认使用旧的 C++ 模式。在设置中启用 C++17 以使用更新的功能。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。