比較簡單的一題,糾結比較久的是把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"; } };