Advertisement
smatskevich

SimpleSort

Mar 30th, 2015
600
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.34 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using std::cin;
  4. using std::vector;
  5.  
  6. struct CPoint {
  7.   int X;
  8.   int Y;
  9.   CPoint() : X( 0 ), Y( 0 ) {}
  10.   CPoint( int x, int y ) : X( x ), Y( y ) {}
  11. };
  12.  
  13. bool pointLess( const CPoint& a, const CPoint& b ) {
  14.   return a.X < b.X;
  15. };
  16.  
  17. // Строго меньше.
  18. bool myLess( const int& a, const int& b ) {
  19.   return a < b;
  20. }
  21.  
  22. template<class T, class Compare>
  23. void SimpleSort( vector<T>& arr, Compare comparator )
  24. {
  25.   for( size_t i = 0; i < arr.size(); ++i ) {
  26.     for( size_t j = 0; j < arr.size() - 1; j++ ) {
  27.       if( comparator( arr[j + 1], arr[j] ) ) {
  28.         std::swap( arr[j], arr[j + 1] );
  29.       }
  30.     }
  31.   }
  32. }
  33.  
  34. int main()
  35. {
  36.   vector<int> arr;
  37.   while( true ) {
  38.     int data = 0;
  39.     cin >> data;
  40.     if( cin.fail() ) {
  41.       break;
  42.     }
  43.     arr.push_back( data );
  44.   }
  45.   SimpleSort( arr, myLess );
  46.   SimpleSort( arr, std::less<int>() );
  47.   for( size_t i = 0; i < arr.size(); ++i ) {
  48.     std::cout << arr[i] << " ";
  49.   }
  50.   std::cout << std::endl;
  51.  
  52.   // ===========================================
  53.  
  54.   vector<CPoint> points;
  55.   points.push_back( CPoint( 5, 2 ) );
  56.   points.push_back( CPoint( 3, 4 ) );
  57.   SimpleSort( points, &pointLess );
  58.  
  59.   for( size_t i = 0; i < points.size(); ++i ) {
  60.     std::cout << points[i].X << " " << points[i].Y << std::endl;
  61.   }
  62.   return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement