I'm trying to create a function to calculate the standard deviation.
I tried using std::inner_product
and lambda expressions for op1 and op2 of inner_product to modify the operations of the std::inner_product
function.
Unfortunately I'm getting a compiler error when calling the function in the main loop:
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>>>> &"
This is my code:
#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));
}
Call in main:
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;
}
I hope you can help me.
The error message means that the type of the argument that is std::vector<float>
std::vector<float> testv {6,3,2,9,11,44,20};
float stdw = stdfunc(testv);
does not correspond to the type of the function parameter that is std::vector<std::vector<float>>
.
So check the function declaration. It is not excluded that the function declaration and the functions definition can differ.