将异步与std ::累积
Using async with std::accumulate
我认为这会更简单,但是尝试了以下许多变体,我无法将此代码编译
#include <thread>
#include <algorithm>
#include <future>
int main()
{
std::vector<int> vec(1000000, 0);
std::future<int> x = std::async(std::launch::async, std::accumulate, vec.begin(), vec.end(), 0);
}
error: no matching function for call to 'async(std::launch, <unresolved overloaded function type>, std::vector<int>::iterator, std::vector<int>::iterator, int)'
我缺少什么?
,因为std::accumulate
是一个模板,您必须在获取地址之前提供模板参数(将其解析为特定函数)。
#include <thread>
#include <algorithm>
#include <future>
#include <vector>
#include <numeric>
int main()
{
std::vector<int> vec(1000000, 0);
std::future<int> x = std::async(std::launch::async,
&std::accumulate<std::vector<int>::const_iterator, int>,
vec.begin(), vec.end(), 0);
}
那是一种uck,所以您可以使用lambda:
std::future<int> x = std::async(std::launch::async,
[&]{ return std::accumulate(vec.begin(), vec.end(), 0); });
相关文章:
- 使用std::multimap迭代器创建std::list
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 如何使用 std::累积在 C++ 中计算总和立方体
- 我可以将向量作为initial_sum和不同的函数传递给 std::累积吗?
- 在模板中奇怪地使用 std::累积
- 无法编译 std::reduce 调用,而 std::累积调用编译相同的参数
- std::transform 可以替换为 std::累积吗?
- 我可以毫无例外地使用 std::累积与易错操作吗?
- 当调用时,为什么ranges ::累积的init as std :: Move(init)
- 如何将对象传递到std ::累积函数
- 将异步与std ::累积
- 可能溢出 std::累积
- TBB :: Parallel_Reduce和STD ::累积的结果不同
- 是否有类似STD ::累积在迭代器上运行的东西
- 如何使用 std::累积 和 lambda 来计算平均值
- 在std::for_each的执行过程中累积结果,该异常被中断
- std::累积BinaryOperator副作用
- 使用 std::累积,得到一个"too many arguments"错误