std::inner_product计算一个向量的标准差

std::inner_product to calculate the standard deviation of a vector

我正在尝试创建一个函数来计算标准偏差。 我尝试对 inner_product 的 op1 和 op2 使用 std::inner_product 和 lambda 表达式来修改 std::inner_product 函数的操作。

不幸的是,我在主循环中调用函数时遇到编译器错误:

error C2664: cannot convert parameter 1 from "std::vector<float,std::allocator<float>>" in "std::vector<std::vector<float,std::allocator<float>>,std::allocator<std::vector<float,std::allocator<float>>>> &"

这是我的代码:

#include <numeric>
#include <cmath>

float stdfunc(std::vector<float> const & invector) {
   float mean = std::accumulate(invector.begin(), invector.end(), 0.0) / invector.size();

   float sq_sum = std::inner_product(invector.begin(), invector.end(), invector.begin(), 0.0,
   [](float const & x, float const & y) {return x+y;},
   [mean](float const & x, float const & y) {return (x-mean)*(y-mean);});

   return std::sqrt(sq_sum / (invector.size() - 1));
}

主线调用:

int main(){
std::vector<float> testv {6,3,2,9,11,44,20};
float stdw = stdfunc(testv);
std::cout << "Standardabw: " << stdw << std::endl;
    return 0;
}

希望你能帮助我。

错误信息的意思是参数的类型是std::vector<float>

std::vector<float> testv {6,3,2,9,11,44,20};
float stdw = stdfunc(testv);

不对应函数参数类型std::vector<std::vector<float>>

所以检查函数声明。不排除函数声明和函数定义可以不同。