·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150 -
-
-
- #include <vector>
- #include <algorithm>
- #include <functional> // For greater<int>( )
- #include <iostream>
- using namespace std;
-
-
- bool UDgreater ( int elem1, int elem2 )
- {
- return elem1 > elem2;
- }
-
- class Csort
- {
- public:
-
-
- bool operator () (int a,int b) const
- {
- return a<b;
- };
- };
-
-
- int main( )
- {
- vector <int> v1;
- vector <int>::iterator Iter1;
- int arr[12];
- int i;
- for ( i = 0 ; i <= 5 ; i++ )
- {
- v1.push_back( 2 * i );
- arr[i]=2*i;
- }
- for ( i = 6 ; i <= 11 ; i++ )
- {
- arr[i]=23-2*i;
- }
-
- int ii;
- for ( ii = 0 ; ii <= 5 ; ii++ )
- {
- v1.push_back( 2 * ii + 1 );
- }
-
- cout << "Original vector v1 = ( " ;
- for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
- cout << *Iter1 << " ";
- cout << ")" << endl;
- cout << "Original array arr = ( " ;
- for ( int n=0;n<12;n++ )
- cout << arr[n] << " ";
- cout << ")" << endl;
-
-
- sort( v1.begin( ), v1.end( ) );
- cout << "Sorted vector v1 = ( " ;
- for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
- cout << *Iter1 << " ";
- cout << ")" << endl;
-
-
-
- sort( &arr[0], &arr[12],UDgreater);
-
- cout << "Resorted (UDgreater) array arr = ( " ;
- for ( int n=0;n<12;n++ ) cout << arr[n] << " ";
- cout << ")" << endl;
-
- sort( &arr[0], &arr[12],Csort());
-
- cout << "Resorted (Csort()) arr = ( " ;
- for ( int n=0;n<12;n++ ) cout << arr[n] << " ";
- cout << ")" << endl;
-
-
- sort( v1.begin( ), v1.end( ), greater<int>( ) );
-
-
- cout << "Resorted (greater) vector v1 = ( " ;
- for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
- cout << *Iter1 << " ";
- cout << ")" << endl;
-
-
-
- sort( v1.begin( ), v1.end( ),Csort());
- cout << "Resorted (UDgreater) vector v1 = ( " ;
- for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
- cout << *Iter1 << " ";
- cout << ")" << endl;
-
- getchar();
- return 0;
- }
|