C++ 欧氏距离的变换向量

C++ 欧氏距离的变换向量,c++,c++11,vector,stl,C++,C++11,Vector,Stl,我想找到计算向量中某些点的欧几里德距离的最佳方法。我尝试使用std::transform对每个数字进行平方运算,然后将此函数作为参数传递到std::acculate。这是最有效的方法吗?到目前为止,我的实现有一个bug,错误是: in instantiation of function template specialization 'std::__1::accumulate<std::__1::__wrap_iter<int *>, int, std::__1::__wrap

我想找到计算向量中某些点的欧几里德距离的最佳方法。我尝试使用std::transform对每个数字进行平方运算,然后将此函数作为参数传递到std::acculate。这是最有效的方法吗?到目前为止,我的实现有一个bug,错误是:

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;
这是我的代码:

#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;
}
您需要先变换,然后累加变换后的向量

std::transform(v.begin(), v.end(), v.begin(), square);                                                                                                       
cout << std::sqrt(std::accumulate(v.begin(), v.end(), 0)) << endl;
as尚未返回范围。如果/when’s能够应用到C++17,您将能够更加简洁优雅地编写此代码

就效率而言,我认为它相当有效。不要担心微优化,除非您从分析器中知道您遇到了代码的热点区域。

您需要首先转换,然后累积转换后的向量

std::transform(v.begin(), v.end(), v.begin(), square);                                                                                                       
cout << std::sqrt(std::accumulate(v.begin(), v.end(), 0)) << endl;
as尚未返回范围。如果/when’s能够应用到C++17,您将能够更加简洁优雅地编写此代码

就效率而言,我认为它相当有效。不要担心微优化,除非您从分析器中知道您遇到了代码的一个热点区域。

这正是它的用途

这正是我们的目的


谢谢问:我注意到的一件事是有一个4参数累加,它接受一个函数作为它的第四个参数。难道不可能在一个步骤中结合累积和转换吗?@nietsnegttiw还没有。。。等待范围,请参阅更新的编辑。@NietsNegattiw第四个参数用于定义累加函数;默认情况下使用+,但您可以使用其他操作。谢谢!问:我注意到的一件事是有一个4参数累加,它接受一个函数作为它的第四个参数。难道不可能在一个步骤中结合累积和转换吗?@nietsnegttiw还没有。。。等待范围,请参阅更新的编辑。@NietsNegattiw第四个参数用于定义累加函数;默认情况下,使用+但您可以使用其他操作。