曼哈顿距离 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 ";
    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;
}

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

原文地址:https://www.cnblogs.com/donggongdechen/p/9533607.html