Advertisement
smatskevich

SortWith3TypesComparators

Oct 10th, 2016
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.92 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. using std::vector;
  5. using std::cin;
  6. using std::cout;
  7.  
  8. struct CPoint {
  9.     int X;
  10.     int Y;
  11.  
  12.     CPoint( int x, int y ) : X( x ), Y( y ) {}
  13. };
  14.  
  15. // Вариант 1. Оператор сравнения. Сравнивает точки по удаленности от (0, 0).
  16. bool operator<( const CPoint& first, const CPoint& second )
  17. {
  18.     return ( first.X * first.X + first.Y * first.Y ) <
  19.         ( second.X * second.X + second.Y * second.Y );
  20. }
  21.  
  22. bool operator>( const CPoint& first, const CPoint& second )
  23. {
  24.     return second < first;
  25. }
  26.  
  27. bool operator<=( const CPoint& first, const CPoint& second )
  28. {
  29.     return !( second < first );
  30. }
  31.  
  32. bool operator>=( const CPoint& first, const CPoint& second )
  33. {
  34.     return !( first < second );
  35. }
  36.  
  37. // Вариант 2. Функция сравнения. Сравнивает точки по удаленности от (0, 0).
  38. bool ComparePointsByDistanceFromZero( const CPoint& first, const CPoint& second )
  39. {
  40.     return ( first.X * first.X + first.Y * first.Y ) <
  41.         ( second.X * second.X + second.Y * second.Y );
  42. }
  43.  
  44. typedef bool ( *TCompareFunction )( const CPoint&, const CPoint& );
  45.  
  46. // Вариант 3. Функтор = класс с оператором ( , ).
  47. // Сравнивает точки по удаленности от некоторой фиксированной.
  48. class CPointsComparator {
  49. public:
  50.     explicit CPointsComparator( const CPoint& _center ) : center( _center ) {}
  51.     bool operator()( const CPoint& first, const CPoint& second ) const;
  52.  
  53. private:
  54.     CPoint center;
  55. };
  56.  
  57. bool CPointsComparator::operator()( const CPoint& first, const CPoint& second ) const
  58. {
  59.     return ( ( first.X - center.X ) * ( first.X - center.X ) +
  60.         ( first.Y - center.Y ) * ( first.Y - center.Y ) ) <
  61.         ( ( second.X - center.X ) * ( second.X - center.X ) +
  62.         ( second.Y - center.Y ) * ( second.Y - center.Y ) );
  63. }
  64.  
  65. // Шаблонная функция с двумя параметрами.
  66. // Вместо COMPARATOR можно передать указатель на функцию, а можно объект-функтор,
  67. // в котором определен оператор () для двух параметров.
  68. template< class T, class COMPARATOR >
  69. void BubbleSort( vector<T>& arr, COMPARATOR cmpFunction )
  70. {
  71.     int n = static_cast< int >( arr.size() );
  72.     for( int i = 0; i < n - 1; ++i ) {
  73.         for( int j = 0; j <= n - i - 2; ++j ) {
  74.             if( cmpFunction( arr[j + 1], arr[j] ) ) {
  75.                 std::swap( arr[j], arr[j + 1] );
  76.             }
  77.         }
  78.     }
  79. }
  80.  
  81. int main()
  82. {
  83.     vector<CPoint> arr;
  84.     while( true ) {
  85.         int x = 0;
  86.         int y = 0;
  87.         if( !( cin >> x >> y ) ) {
  88.             break;
  89.         }
  90.         arr.push_back( CPoint( x, y ) );
  91.     }
  92.  
  93.     CPointsComparator comparator( CPoint( 3, 3 ) );
  94.     BubbleSort<CPoint, CPointsComparator>( arr, comparator );
  95.     for( int i = 0; i < static_cast<int>( arr.size() ); ++i ) {
  96.         cout << arr[i].X << ' ' << arr[i].Y << std::endl;
  97.     }
  98.     return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement