fork download
  1. #include <iostream>
  2. #include <iterator>
  3. #include <vector>
  4. #include <map>
  5. #include <list>
  6. #include <string>
  7. #include <algorithm>
  8. using namespace std;
  9. int times;
  10. class Point
  11. {
  12. private:
  13. int x,y;
  14. public:
  15. Point() : x(0),y(0){}
  16. Point(int x,int y):x(x),y(y){}
  17. Point(const Point& p) : x(p.x),y(p.y) { cout << "copying" <<x<<","<<y<<endl;times++;}
  18. double slopeTo(const Point& that) const
  19. {
  20. if (x == that.x && y == that.y) return - numeric_limits<double>::infinity();
  21. if (x == that.x) return numeric_limits<double>::infinity();
  22. if (y == that.y) return 0;
  23. return (that.y - y) / (double)(that.x - x);
  24. }
  25. bool operator< (const Point& that)const
  26. {
  27.  
  28. if (y < that.y) return true;
  29. if (y == that.y && x < that.x) return true;
  30. return false;
  31. }
  32. friend ostream& operator<< (ostream&, const Point& p);
  33. };
  34.  
  35. class cmpBySlope
  36. {
  37. private:
  38. Point& origin;
  39. public:
  40. cmpBySlope(const cmpBySlope& other)
  41. :origin(other.origin)
  42. {
  43. cout << "cmpBySlope copy!" << endl;
  44. }
  45. cmpBySlope(Point& a) : origin(a){}
  46. bool operator() (const Point* left,const Point* right)const
  47. {
  48. return origin.slopeTo(*left) < origin.slopeTo(*right);
  49. }
  50.  
  51. };
  52. ostream& operator<< (ostream& out, const Point& p)
  53. {
  54. cout << "(" << p.x << "," << p.y << ")" ;
  55. return out;
  56. }
  57.  
  58.  
  59. int N;
  60. vector<Point*> v;
  61. void create()
  62. {
  63. v.push_back(new Point(28000,1000));
  64. v.push_back(new Point(28000,5000));
  65. v.push_back(new Point(28000,13500));
  66. v.push_back(new Point(23000,16000));
  67. v.push_back(new Point(1000,18000));
  68. v.push_back(new Point(13000,21000));
  69. v.push_back(new Point(2000,22000));
  70. v.push_back(new Point(3000,26000));
  71. v.push_back(new Point(3500,28000));
  72. v.push_back(new Point(4000,30000));
  73. cout << "During initialization : " << times << endl;
  74.  
  75. cout << "Input reading has complete!" << endl;
  76. }
  77. bool cmp(const Point* p,const Point* q)
  78. {
  79. return (*p)<(*q);
  80. }
  81. int main(void)
  82. {
  83. create();
  84.  
  85. int before = times;
  86. cout << "Sort by natural order : ";
  87. sort(v.begin(),v.end(),cmp);
  88. for (Point* p : v)
  89. cout << *p << " ";
  90. cout << endl;
  91. cout << "During soring : " << (times - before) << endl;
  92.  
  93. cout << "Sort by slope : ";
  94. before = times;
  95. sort(v.begin(),v.end(),cmpBySlope(*v[2]));
  96. for (Point* p : v)
  97. {
  98. cout << *p << " ";
  99. }
  100. cout << endl;
  101. cout << "During soring : " << (times - before) << endl;
  102. }
Success #stdin #stdout 0s 3440KB
stdin
Standard input is empty
stdout
During initialization : 0
Input reading has complete!
Sort by natural order : (28000,1000) (28000,5000) (28000,13500) (23000,16000) (1000,18000) (13000,21000) (2000,22000) (3000,26000) (3500,28000) (4000,30000) 
During soring : 0
Sort by slope : cmpBySlope copy!
cmpBySlope copy!
cmpBySlope copy!
cmpBySlope copy!
cmpBySlope copy!
cmpBySlope copy!
cmpBySlope copy!
cmpBySlope copy!
cmpBySlope copy!
cmpBySlope copy!
cmpBySlope copy!
(28000,13500) (4000,30000) (3500,28000) (23000,16000) (13000,21000) (3000,26000) (2000,22000) (1000,18000) (28000,1000) (28000,5000) 
During soring : 0