我编写了一个程序,该程序在n维空间中生成100个点并计算质心(几何中心)。它工作正常,但我需要编写此部分而不使用for循环,而只使用累加,for_each和算术函子。
int dimensions = 2;
int number_of_points = 100;
vector<vector<double>> cloud_A(number_of_points, vector<double>(dimensions, 0));
for_each(cloud_A.begin(), cloud_A.end(), RandomGenerator(5, 10));
vector<double> centroid_A(dimensions, 0);
for (int i = 0; i < dimensions; i++){
centroid_A[i] = accumulate(cloud_A.begin(), cloud_A.end(), 0.0, Sum(i));
}
for_each(centroid_A.begin(), centroid_A.end(), Divide(number_of_points));
我呆了几个小时,想知道是否有可能,cloud_A.begin()[count]
是否可行,我最接近的选择是:
int count = 0;
for_each(centroid_A.begin(), centroid_A.end(), [&](double d){
for_each(cloud_A.begin()[count], cloud_A.end()[count], [&](double punkt) {
centroid_A[count]=accumulate(punkt.begin(), punkt.end(), 0.0, plus<double>()) << endl;
});
count++;
});