小编Rob*_*iam的帖子

使用 std::async c++11 返回 std:vector

我在使用 std::future 和 std::async 返回 std::vector 时遇到问题。为什么

#include <iostream>
#include <vector>
#include <functional>
#include <future>

using namespace std;

int main(int argc, char** argv) {

  int A = 10;
  vector<future<int>> sumarray(A);

  for(int i = 0; i < A; i++) 
    sumarray[i] = async( launch::async, [i] { return i; } );

  for(int i = 0; i < A; i++) 
    cout << sumarray[i].get() << " "; 
  cout << endl;

}
Run Code Online (Sandbox Code Playgroud)

用 g++ -std=c++11 -pthread 编译,按预期打印

0 1 2 3 4 5 6 7 8 …

c++ vector c++11

3
推荐指数
1
解决办法
4690
查看次数

标签 统计

c++ ×1

c++11 ×1

vector ×1