比较简单的一题,纠结比较久的是把my_cmp和my_minus放在类中,利用sort函数会出现
no matching function for call to ""sort(std::vector<Interval>::iterator, std::vector<Interval>::iterator, <unresolved overloaded function type>)""
当把这两个函数放在类外面时就行了
#include <iostream> #include <vector> #include <utility> #include <algorithm> #include <cmath> #include <numeric> using namespace std; typedef pair<int,int> Point; bool my_cmp(const Point& a, const Point& b){ return a.second < b.second; } Point my_minus( Point&a, Point& b){ return Point(a.first,a.second-b.second); } class CatchTheBeatEasy{ public: string ableToCatchAll(vector<int> x, vector<int> y){ vector<Point> points; for(int i = 0 ; i < x.size(); ++ i) points.push_back(make_pair(x[i],y[i])); sort(points.begin(),points.end(),my_cmp); adjacent_difference(points.begin(),points.end(),points.begin(),my_minus); int start = 0; for(int i = 0 ; i< points.size(); ++i){ if(abs(points[i].first-start) > points[i].second) return "Not able to catch"; start = points[i].first; } return "Able to catch"; } };