CHAPTER11-27.cpp
上传用户:fjc899
上传日期:2007-07-03
资源大小:187k
文件大小:4k
- //文件名:CHAPTER11-27.cpp
- #include <vector>
- #include <algorithm>
- #include <functional> // For greater<int>( )
- #include <iostream>
- // Return whether modulus of elem1 is less than modulus of elem2
- bool mod_lesser ( int elem1, int elem2 )
- {
- if (elem1 < 0) elem1 = - elem1;
- if (elem2 < 0) elem2 = - elem2;
- return elem1 < elem2;
- }
- int main( )
- {
- using namespace std;
- vector <int> v1a, v1b, v1 ( 12 );
- vector <int>::iterator Iter1a, Iter1b, Iter1;
- // Constructing vector v1a and v1b with default less than ordering
- int i;
- for ( i = 0 ; i <= 5 ; i++ ) {v1a.push_back( i );}
- int ii;
- for ( ii =-5 ; ii <= 0 ; ii++ ) {v1b.push_back( ii );}
- cout << "Original vector v1a with range sorted by then "
- << "binary predicate less than is v1a = ( " ;
- for ( Iter1a = v1a.begin( ) ; Iter1a != v1a.end( ) ; Iter1a++ ) cout << *Iter1a << " ";
- cout << ")." << endl;
- cout << "Original vector v1b with range sorted by then "
- << "binary predicate less than is v1b = ( " ;
- for ( Iter1b = v1b.begin ( ) ; Iter1b != v1b.end ( ) ; Iter1b++ ) cout << *Iter1b << " ";
- cout << ")." << endl;
- // Constructing vector v2 with ranges sorted by greater
- vector <int> v2a ( v1a ) , v2b ( v1b ) , v2 ( v1 );
- vector <int>::iterator Iter2a, Iter2b, Iter2;
- sort ( v2a.begin ( ) , v2a.end ( ) , greater<int> ( ) );
- sort ( v2b.begin ( ) , v2b.end ( ) , greater<int> ( ) );
- cout << "Original vector v2a with range sorted by then "
- << "binary predicate greater is v2a = ( " ;
- for ( Iter2a = v2a.begin ( ) ; Iter2a != v2a.end ( ) ; Iter2a++ ) cout << *Iter2a << " ";
- cout << ")." << endl;
- cout << "Original vector v2b with range sorted by then "
- << "binary predicate greater is v2b = ( " ;
- for ( Iter2b = v2b.begin ( ) ; Iter2b != v2b.end ( ) ; Iter2b++ )cout << *Iter2b << " ";
- cout << ")." << endl;
- // Constructing vector v3 with ranges sorted by mod_lesser
- vector <int> v3a ( v1a ), v3b ( v1b ) , v3 ( v1 );
- vector <int>::iterator Iter3a, Iter3b, Iter3;
- sort ( v3a.begin ( ) , v3a.end ( ) , mod_lesser );
- sort ( v3b.begin ( ) , v3b.end ( ) , mod_lesser );
- cout << "Original vector v3a with range sorted by then "
- << "binary predicate mod_lesser is v3a = ( " ;
- for ( Iter3a = v3a.begin ( ) ; Iter3a != v3a.end ( ) ; Iter3a++ )cout << *Iter3a << " ";
- cout << ")." << endl;
- cout << "Original vector v3b with range sorted by then "
- << "binary predicate mod_lesser is v3b = ( " ;
- for ( Iter3b = v3b.begin ( ) ; Iter3b != v3b.end ( ) ; Iter3b++ ) cout << *Iter3b << " ";
- cout << ")." << endl;
- // To merge inplace in ascending order with default binary predicate less <int> ( )
- merge ( v1a.begin ( ) , v1a.end ( ) , v1b.begin ( ) , v1b.end ( ) , v1.begin ( ) );
- cout << "Merged inplace with default order,n vector v1mod = ( " ;
- for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )cout << *Iter1 << " ";
- cout << ")." << endl;
- // To merge inplace in descending order, specify binary predicate greater<int>( )
- merge ( v2a.begin ( ) , v2a.end ( ) , v2b.begin ( ) , v2b.end ( ) , v2.begin ( ) , greater <int> ( ) );
- cout << "Merged inplace with binary predicate greater specified,n "<< "vector v2mod = ( " ;
- for ( Iter2 = v2.begin( ) ; Iter2 != v2.end( ) ; Iter2++ ) cout << *Iter2 << " ";
- cout << ")." << endl;
- // Applying A user-defined (UD) binary predicate mod_lesser
- merge ( v3a.begin ( ) , v3a.end ( ) , v3b.begin ( ) , v3b.end ( ) , v3.begin ( ) , mod_lesser );
- cout << "Merged inplace with binary predicate mod_lesser specified,n "
- << "vector v3mod = ( " ; ;
- for ( Iter3 = v3.begin( ) ; Iter3 != v3.end( ) ; Iter3++ ) cout << *Iter3 << " ";
- cout << ")." << endl;
- }