Advertisement
uopspop

Untitled

Apr 28th, 2016
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.09 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. using namespace std;
  4.  
  5. bool compare(int a, int b){
  6.     if( a > b ){
  7.         return true;
  8.     }
  9.     else {
  10.         return false;
  11.     }
  12. }
  13.  
  14. typedef struct{
  15.   bool operator() (int i,int j) { return (i<j);}
  16. }Mystruct;
  17.  
  18.  
  19. class MyClass{
  20. public:
  21.     bool operator()(int a, int b){
  22.         return (a>b);
  23.     }
  24. };
  25.  
  26.  
  27. int main(){
  28.     int array[] = {5, 3, 1, 2, 4};
  29.    
  30. // default:    small to large
  31.     sort(array, array+5);      
  32.     for( int i = 0 ; i < 5 ; i++ ){
  33.         cout << array[i] << " ";
  34.     }
  35.     cout << endl << endl;
  36.  
  37. // customed function: large to small
  38.     sort(array, array+5, compare);
  39.     for( int i = 0 ; i < 5 ; i++ ){
  40.         cout << array[i] << " ";
  41.     }
  42.     cout << endl << endl;
  43.  
  44.  
  45. // customed object(struct): small to large
  46.     Mystruct myobject;
  47.     sort(array, array+5, myobject);
  48.     for( int i = 0 ; i < 5 ; i++ ){
  49.         cout << array[i] << " ";
  50.     }
  51.     cout << endl << endl;
  52.  
  53. // customed object(class): small to large
  54.     MyClass myobject2;
  55.     sort(array, array+5, myobject2);
  56.     for( int i = 0 ; i < 5 ; i++ ){
  57.         cout << array[i] << " ";
  58.     }
  59.     cout << endl << endl;
  60.  
  61.    
  62.     return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement