CHAPTER11-22.cpp
上传用户:fjc899
上传日期:2007-07-03
资源大小:187k
文件大小:2k
- //文件名:CHAPTER11-22.cpp
- #include <list>
- #include <vector>
- #include <algorithm>
- #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;
- list <int> L;
- list <int>::iterator Iter;
- bool b1, b2;
- L.push_back( 50 );
- L.push_back( 10 );
- L.push_back( 30 );
- L.push_back( 20 );
- L.push_back( 25 );
- L.push_back( 5 );
- L.sort( );
- cout << "L = ( " ;
- for ( Iter = L.begin( ) ; Iter != L.end( ) ; Iter++ )
- cout << *Iter << " ";
- cout << ")" << endl;
- b1 = binary_search( L.begin( ), L.end( ), 10 );
- if ( b1 )
- cout << "There is an element in list L with a value equal to 10."<< endl;
- else
- cout << "There is no element in list L with a value equal to 10."<< endl;
- // a binary_search under the binary predicate greater
- L.sort ( greater<int> ( ) );
- b2 = binary_search( L.begin( ), L.end( ), 10 , greater<int> ( ) );
- if ( b2 )
- cout << "There is an element in list L with a value equivalent to 10 "
- << "under greater than." << endl;
- else
- cout << "No element in list L with a value equivalent to 10 "<< "under greater than." << endl;
- // a binary_search under the user-defined binary predicate mod_lesser
- vector <int> v1;
- vector <int>::iterator Iter1;
- int i;
- for ( i = -2 ; i <= 4 ; i++ )
- { v1.push_back( i ); }
- sort ( v1.begin ( ) , v1.end ( ) , mod_lesser );
- cout << "Ordered under mod_lesser, vector v1 = ( " ;
- for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
- cout << *Iter1 << " ";
- cout << ")" << endl;
- bool b3 = binary_search( v1.begin( ), v1.end( ), -3 , mod_lesser );
- if ( b3 )
- cout << "There is an element with a value equivalent to -3 "<< "under mod_lesser." << endl;
- else
- cout << "There is not an element with a value equivalent to -3 "
- << "under mod_lesser." << endl;
- }