cpp study

来源:互联网 发布:java软件开发软件下载 编辑:程序博客网 时间:2024/05/17 08:09
#include <iostream>#include <iterator>#include <vector>#include <map>#include <list>#include <string>#include <algorithm>using namespace std;class Point{private:int x,y;public:Point(int x,int y):x(x),y(y){}double slopeTo(const Point& that) const{if (x == that.x && y == that.y) return - numeric_limits<double>::infinity();if (x == that.x) return numeric_limits<double>::infinity();if (y == that.y) return 0;return (that.y - y) / (double)(that.x - x);}bool operator< (const Point& that)const{if (y < that.y) return true;if (y == that.y && x < that.x) return true;return false;}friend ostream& operator<< (ostream&, Point& p);};class cmpBySlope{private:Point origin;public:cmpBySlope(Point& a) : origin(a){}bool operator() (const Point& left,const Point& right)const{return origin.slopeTo(left) < origin.slopeTo(right);}};ostream& operator<< (ostream& out, Point& p){cout << "(" << p.x << "," << p.y << ")" ;return out;}int N;vector<Point> v;void create(){cin >> N;for (int i = 0 ; i < N; i++){int x,y;cin >> x >> y;Point* p = new Point(x,y);v.push_back(*p);}cout << "Input reading has complete!" << endl;}int main(void)        {create();cout << "Sort by natural order : ";sort(v.begin(),v.end());for (Point& p : v)cout << p << " ";cout << endl;cout << "Sort by slope : ";sort(v.begin(),v.end(),cmpBySlope(v[2]));for (Point& p : v){cout << p << " ";}cout << endl;}            

0 0
原创粉丝点击