I would like to find the optimal approach to computing the Euclidean distance of some points in a vector. I am trying to use a std::transform to square each number, then pass this function as an argument into std::accumulate. Is this the most efficient way? As of right now my implementation has a bug, the error is:
in instantiation of function template specialization 'std::__1::accumulate<std::__1::__wrap_iter<int *>, int, std::__1::__wrap_iter<int *> >' requested
here
cout << std::sqrt(std::accumulate(v.begin(), v.end(), 0, std::transform(v.begin(), v.end(), v.begin(), square))) << endl;
This is my code:
#include<iostream>
#include<vector>
#include<algorithm>
#include<numeric>
#include<cmath>
using std::cout;
using std::endl;
using std::string;
int square(int n) {return n*n;}
int
main()
{
std::vector<int> v;
std::vector<int> v1;
for(int i = 0; i < 10; ++i)
{v.push_back(i);}
v1.resize(v.size());
//std::transform (v.begin(), v.end(), v1.begin(), square);
//std::copy(v.begin(), v1.begin(), std::ostream_iterator<int>(cout, ", "));
//cout << std::sqrt(std::accumulate(v1.begin(), v1.end(), 0)) << endl;
cout << std::sqrt(std::accumulate(v.begin(), v.end(), 0, std::transform(v.begin(), v.end(), v.begin(), square))) << endl;
//for(auto it = v1.begin(); it != v1.end(); it++)
//{cout << *it << endl;}
return 0;
}
Aucun commentaire:
Enregistrer un commentaire