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

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

posted @ 2018-08-25 12:56  东宫得臣  阅读(1663)  评论(0编辑  收藏  举报