Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- using namespace std;
- bool compare(int a, int b){
- if( a > b ){
- return true;
- }
- else {
- return false;
- }
- }
- typedef struct{
- bool operator() (int i,int j) { return (i<j);}
- }Mystruct;
- class MyClass{
- public:
- bool operator()(int a, int b){
- return (a>b);
- }
- };
- int main(){
- int array[] = {5, 3, 1, 2, 4};
- // default: small to large
- sort(array, array+5);
- for( int i = 0 ; i < 5 ; i++ ){
- cout << array[i] << " ";
- }
- cout << endl << endl;
- // customed function: large to small
- sort(array, array+5, compare);
- for( int i = 0 ; i < 5 ; i++ ){
- cout << array[i] << " ";
- }
- cout << endl << endl;
- // customed object(struct): small to large
- Mystruct myobject;
- sort(array, array+5, myobject);
- for( int i = 0 ; i < 5 ; i++ ){
- cout << array[i] << " ";
- }
- cout << endl << endl;
- // customed object(class): small to large
- MyClass myobject2;
- sort(array, array+5, myobject2);
- for( int i = 0 ; i < 5 ; i++ ){
- cout << array[i] << " ";
- }
- cout << endl << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement