曼哈顿距离 C++

曼哈顿距离 C++,第1张

曼哈顿距离 C++

template <class T1, class T2>
double ManhattanDistance(std::vector<T1> &inst1, std::vector<T2> &inst2) {
  if(inst1.size() != inst2.size()) {
    std::cout<<"the size of the vectors is not the same\n";
    return -1;
  }
  std::vector<double> temp;
  for(size_t i=0;i<inst1.size();++i) {
    temp.push_back(std::abs(inst1.at(i)-inst2.at(i)));
  }
  double distance=accumulate(temp.begin(), temp.end(), 0.0);

  return distance;
}

曼哈顿距离,又称为城市街区距离。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/zaji/587863.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-12
下一篇 2022-04-12

发表评论

登录后才能评论

评论列表(0条)

保存