c ++ 11中的未来向量
vector of future in c++11
您好,我使用 lambda 函数在 C++11 中创建了一个未来向量。
vector<double> v = { 0, 1.1, 2.2, 3.3, 4.4, 5.5 };
auto K = [=](double z){
double y=0;
for (const auto x : v)
y += x*x*z;
return y;
};
vector<future<double>> VF;
for (double i : {1,2,3,4,5,6,7,8,9})
VF.push_back(async(K,i));
它运行成功,但是当我尝试通过 for_each 调用检索值时,我遇到了一个我不理解的编译错误。
for_each(VF.begin(), VF.end(), [](future<double> x){cout << x.get() << " "; });
值已通过旧式 for 循环成功获取:
for (int i = 0; i < VF.size(); i++)
cout << VF[i].get() << " ";
为什么我无法使用 for_each 功能?我正在使用 Visual Studio 2013 并尝试使用 INTEL (V16) 编译器。
您不能跟单期货。
要么使用引用,要么存储一个 shared_future
。
future的拷贝构造函数被删除,不能拷贝。使用参考:
for_each(VF.begin(), VF.end(), [](future<double>& x){cout << x.get() << " "; });
^~~~~ !
下面是使用两个合法选项之一的测试代码:
#include <vector>
#include <future>
#include <iostream>
#include <algorithm>
using namespace std;
// option 1 : pass a reference to the future
void test1()
{
vector<double> v = { 0, 1.1, 2.2, 3.3, 4.4, 5.5 };
auto K = [=](double z){
double y=0;
for (const auto x : v)
y += x*x*z;
return y;
};
vector<future<double>> VF;
for (double i : {1,2,3,4,5,6,7,8,9})
VF.push_back(async(K,i));
for_each(VF.begin(), VF.end(), [](future<double>& x){cout << x.get() << " "; });
}
// option 2 : store shared_futures which allow passing copies
void test2()
{
vector<double> v = { 0, 1.1, 2.2, 3.3, 4.4, 5.5 };
auto K = [=](double z){
double y=0;
for (const auto x : v)
y += x*x*z;
return y;
};
vector<shared_future<double>> VF;
for (double i : {1,2,3,4,5,6,7,8,9})
VF.push_back(async(K,i));
for_each(VF.begin(), VF.end(), [](shared_future<double> x){cout << x.get() << " "; });
}
您好,我使用 lambda 函数在 C++11 中创建了一个未来向量。
vector<double> v = { 0, 1.1, 2.2, 3.3, 4.4, 5.5 };
auto K = [=](double z){
double y=0;
for (const auto x : v)
y += x*x*z;
return y;
};
vector<future<double>> VF;
for (double i : {1,2,3,4,5,6,7,8,9})
VF.push_back(async(K,i));
它运行成功,但是当我尝试通过 for_each 调用检索值时,我遇到了一个我不理解的编译错误。
for_each(VF.begin(), VF.end(), [](future<double> x){cout << x.get() << " "; });
值已通过旧式 for 循环成功获取:
for (int i = 0; i < VF.size(); i++)
cout << VF[i].get() << " ";
为什么我无法使用 for_each 功能?我正在使用 Visual Studio 2013 并尝试使用 INTEL (V16) 编译器。
您不能跟单期货。
要么使用引用,要么存储一个 shared_future
。
future的拷贝构造函数被删除,不能拷贝。使用参考:
for_each(VF.begin(), VF.end(), [](future<double>& x){cout << x.get() << " "; });
^~~~~ !
下面是使用两个合法选项之一的测试代码:
#include <vector>
#include <future>
#include <iostream>
#include <algorithm>
using namespace std;
// option 1 : pass a reference to the future
void test1()
{
vector<double> v = { 0, 1.1, 2.2, 3.3, 4.4, 5.5 };
auto K = [=](double z){
double y=0;
for (const auto x : v)
y += x*x*z;
return y;
};
vector<future<double>> VF;
for (double i : {1,2,3,4,5,6,7,8,9})
VF.push_back(async(K,i));
for_each(VF.begin(), VF.end(), [](future<double>& x){cout << x.get() << " "; });
}
// option 2 : store shared_futures which allow passing copies
void test2()
{
vector<double> v = { 0, 1.1, 2.2, 3.3, 4.4, 5.5 };
auto K = [=](double z){
double y=0;
for (const auto x : v)
y += x*x*z;
return y;
};
vector<shared_future<double>> VF;
for (double i : {1,2,3,4,5,6,7,8,9})
VF.push_back(async(K,i));
for_each(VF.begin(), VF.end(), [](shared_future<double> x){cout << x.get() << " "; });
}