gpt4 book ai didi

c++ - 在 C++ 中使用 reduce() 和 std::execution 时出现编译错误

转载 作者:行者123 更新时间:2023-12-01 14:23:00 25 4
gpt4 key购买 nike

我正在尝试实现一个简单的用例 reduce()std::execution。我正在为以下代码使用 C++ 17 标准。但是,下面的代码仍然没有编译。

#include <iostream>
#include <numeric>
#include <vector>
#include <algorithm>
#include <execution>

int main() {
std:: vector<int> vec(10);
std:: iota(std::begin(vec), std::end(vec), 1);
const auto result = std::reduce(std::execution::par, std::begin(vec),
std::end(vec));
std:: cout << result;
return 0;
}

错误:

C:\Users\Blind1729\Desktop>g++ --std=c++17 a.cpp
a.cpp: In function 'int main()':
a.cpp:8:25: error: 'reduce' is not a member of 'std'
const auto result = std::reduce(std::execution::par, std::begin(vec),
^
a.cpp:8:42: error: 'std::execution' has not been declared
const auto result = std::reduce(std::execution::par, std::begin(vec),

最佳答案

您需要针对 TBB 进行链接,因为它依赖于并行 STD(请参阅 C++17 STL Parallel Algorithms - with GCC 9.1 and Intel TBB on Linux and macOS ):

g++ --std=c++17 a.cpp -ltbb

并确保您至少安装了 GCC 9.1。

关于c++ - 在 C++ 中使用 reduce() 和 std::execution 时出现编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61564006/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com