如何在 c++ 11 futures 中多次使用 get() 或如何获取向量值
How to use get() multiple times in c++ 11 future or how to get a vector values?
>我有一个函数,什么是向量返回
std::vector<int> makecode(std::vector<std::string> row)
和我的程序:
std::vector<std::vector<std::string>> data(n);
std::vector<std::future<std::vector<int>>> results(n);
for(size_t i =0;i<n;++i){
results.push_back(std::async(std::launch::async,makecode, data[i]));
}
for(std::future<std::vector<int>>& f : results){
f.wait();
f.get();;
}
我得到这个异常:
what(): No associated state Error...
是的,我不能多次使用 get,所以我使用results.push_back(std::move(f));
行,如果我不f.wait()
行发表评论,结果是相同的错误。
除了这个,一切都在工作。 如何获取由我的"生成代码"函数制作的向量?
创建vector
时,使用n
元素对其进行初始化。这些未来与任何东西都没有关联,所以当你试图wait
它们时,他们会抛出一个异常。要修复,请更改:
std::vector<std::future<std::vector<int>>> results(n);
自
std::vector<std::future<std::vector<int>>> results;
或者分配给每个元素而不是调用push_back
:
std::vector<std::future<std::vector<int>>> results(n);
for(size_t i =0;i<n;++i){
results[i] = std::async(std::launch::async,makecode, data[i]);
}
相关文章:
- 获取向量C++中第一个值和最后一个值的和
- 从 exprTk 获取向量作为输出
- C++ 获取向量中常量字符* [ ] 的长度
- 如何使用模板获取向量中的数据类型?
- C++ 通过 std::transform 的 vector 索引获取向量
- C++ 运算符"="重载 - 在 lhs 上获取向量中的所有值,使其等于 rhs 上的双精度值
- 通过交互器获取向量中的索引
- 无法获取向量中元素的总和
- 获取向量中元素的地址
- 如何获取向量的每个第 N 个元素的"address"
- 我应该如何从特征 3 中的张量切片中获取向量
- 如何获取向量的排序索引
- 向量C ,获取向量的第二个元素有什么问题
- 获取向量<float>中 k 个最大值的 indize 的有效方法
- 获取向量的指定元素
- 如何在具有相同基类的两个不同类中获取向量
- 如何获取向量的这些元素,其中另一个向量在相应的索引处具有'1
- 一个模板函数,用于获取向量中的最大值及其相应的索引
- 获取向量 c++ 中的字符串计数
- C++linux获取向量迭代器不兼容