仿函数
一元
template < class T , class R >
struct unary_function
{
using arguement_type = T;
using result_type = R;
} ;
template < class T1 , class T2 , class R >
struct binary_function
{
using first_argument_type = T1;
using second_argument_type = T2;
using result_type = R;
} ;
6个算术仿函数
template < class T >
struct plus : public binary_function < T , T , T >
{
T operator ( ) ( const T& x, const T& y) const
{
return x + y;
}
} ;
template < class T >
struct minus : public binary_function < T , T , T >
{
T operator ( ) ( const T& x, const T& y) const
{
return x - y;
}
} ;
template < class T >
struct multiplies : public binary_function < T , T , T >
{
T operator ( ) ( const T& x, const T & y) const
{
return x * y;
}
} ;
template < class T >
struct divide : public binary_function < T , T , T >
{
T operator ( ) ( const T& x, const T& y) const
{
return x / y;
}
} ;
template < class T >
struct modulus : public binary_function < T , T , T >
{
T operator ( ) ( const T& x, const T& y) const
{
return x % y;
}
} ;
template < class T >
struct negate : public binary_function < T , T , T >
{
T operator ( ) ( const T& x) const
{
return - x;
}
} ;
template < class T >
inline T identify_element ( plus< T> )
{
return T ( 0 ) ;
}
template < class T >
inline T identify_element ( multiplies< T> )
{
return T ( 1 ) ;
}
template < class T >
struct equal_to : public binary_function < T , T , bool > {
bool operator ( ) ( const T & x, const T & y) const { return x == y; }
} ;
template < class T >
struct not_equal_to : public binary_function < T , T , bool >
{
bool operator ( ) ( const T& x, const T& y) const
{
return x != y;
}
} ;
template < class T >
struct greater : public binary_function < T , T , bool >
{
bool operator ( ) ( const T& x, const T& y) const
{
return x > y;
}
} ;
template < class T >
struct less : public binary_function < T , T , bool >
{
bool operator ( ) ( const T& x, const T& y) const
{
return x < y;
}
} ;
template < class T >
struct greater_equal : public binary_function < T , T , bool >
{
bool operator ( ) ( const T& x, const T& y) const
{
return x >= y;
}
} ;
template < class T >
struct less_equal : public binary_function < T , T , bool >
{
bool operator ( ) ( const T& x, const T& y) const
{
return x <= y;
}
} ;
template < class T >
struct logical_and : public binary_function < T , T , bool >
{
bool operator ( ) ( const T& x, const T& y) const
{
return x && y;
}
} ;
template < class T >
struct logical_or : public binary_function < T , T , bool >
{
bool operator ( ) ( const T& x, const T& y) const
{
return x || y;
}
} ;
template < class T >
struct logical_not : public binary_function < T , T , bool >
{
bool operator ( ) ( const T& x) const
{
return ! x;
}
} ;
template < class T >
struct identify : public unary_function < T , T >
{
const T& operator ( ) ( const T& x) const
{
return x;
}
} ;
template < class T >
struct select1st : public unary_function < T , typename T:: first_type >
{
const typename T :: first_type& operator ( ) ( const T& x)
{
return x. first;
}
} ;
template < class T >
struct select2nd : public unary_function < T , typename T:: second_type >
{
const typename T :: second_type& operator ( ) ( const T& x)
{
return x. second;
}
} ;
template < class T , class R >
struct project1st : public binary_function < T , R , T >
{
T operator ( ) ( const T& x, const R& y) const
{
return x;
}
} ;
template < class T , class R >
struct project2nd : public binary_function < T , R , T >
{
R operator ( ) ( const T& x, const T& y) const
{
return y;
}
} ;
template < class T >
struct unary_negate : public unary_function < typename T:: argument_type , bool >
{
protected :
T pred;
public :
explicit unary_negate ( const T& x) : pred ( x) { }
bool operator ( ) ( const typename T :: arguemnt_type& x) const
{
return ! pred ( x) ;
}
} ;
template < class T >
inline unary_negate< T> not1 ( const T & pred)
{
return unary_negate < T> ( pred) ;
}
template < class T >
struct binary_negate : public binary_function < typename T:: first_argument_type ,
typename T:: second_argument_type , bool >
{
protected :
T pred;
public :
explicit binary_negate ( const T& x) : pred ( x) { }
bool operator ( ) ( const typename T :: first_argument_type & x,
const typename T :: second_argument_type & y)
{
return ! pred ( x, y) ;
}
} ;
template < class T >
inline binary_negate< T> not2 ( const T& pred)
{
return binary_negate < T> ( pred) ;
}
template < class T >
struct binder1st : public unary_function < typename T:: second_argument_tyep ,
typename T:: result_type >
{
protected :
T op;
typename T :: first_argument_type value;
public :
binder1st ( const T& x, const typename T :: first_arguement_type & y)
: op ( x) , value ( y) { }
typename T :: result_type
operator ( ) ( const typename T :: second_argument_type& x) const
{
return op ( value, x) ;
}
} ;
template < class T , class R >
inline binder1st< T> bind1st ( const T& op, const R& x)
{
using Arg1_Type = typename T :: first_argument_type;
return binder1st < T> ( op, static_cast < Arg1_Type> ( x) ) ;
}
template < class T >
struct binder2nd : public unary_function < typename T:: first_argument_type ,
typename T:: result_type >
{
protected :
T op;
typename T :: second_argument_type value;
public :
binder2nd ( const T& x, const typename T :: second_argument_type & y)
: op ( x) , value ( y) { }
typename T :: result_type
operator ( ) ( const typename T :: first_argument_type & x) const
{
return op ( x, value) ;
}
} ;
template < class T , class R >
inline binder2nd< T> bind2nd ( const T& op, const R& x)
{
using Arg2_type = typename T :: second_argument_type;
return binder2nd < T> ( op, Arg2_type ( x) ) ;
}
template < class T , class R >
struct unary_compose : public unary_function < typename R:: argument_type ,
typename T:: result_type >
{
protected :
T op1;
R op2;
public :
unary_compose ( const T& x, const R& y) : op1 ( x) , op2 ( y) { }
typename T :: result_type operator ( ) ( const typename R :: argument_type& x) const
{
return op1 ( op2 ( x) ) ;
}
} ;
template < class T1 , class T2 >
inline unary_compose< T1, T2> compose1 ( const T1& op1, const T2& op2)
{
return unary_compose < T1, T2> ( op1, op2) ;
}
template < class T1 , class T2 , class T3 >
struct binary_compose : public unary_function < typename T2:: argument , typename T1:: result_type >
{
protected :
T1 op1;
T2 op2;
T3 op3;
public :
binary_compose ( const T1& x, const T2& y, const T3& z) : op1 ( x) , op2 ( y) , op3 ( z) { }
typename T1 :: result_type operator ( ) ( const typename T2 :: argument_type& x) const
{
return op1 ( op2 ( x) , op3 ( x) ) ;
}
} ;
template < class T1 , class T2 , class T3 >
inline binary_compose< T1, T2, T3> compose2 ( const T1& op1, const T2& op2, const T3& op3)
{
return binary_compose < T1, T2, T3> ( op1, op2, op3) ;
}
template < class T , class R >
struct pointer_to_unary_funtion : public unary_function < T , R >
{
protected :
R ( * ptr) ( T) ;
public :
pointer_to_unary_funtion ( ) { }
explicit pointer_to_unary_funtion ( R ( * x) ( T) ) : ptr ( x) { }
R operator ( ) ( T x) const
{
return ptr ( x) ;
}
} ;
template < class T , class R >
inline pointer_to_unary_funtion< T, R> ptr_fun ( R ( * x) ( T) )
{
return pointer_to_unary_funtion < T, R> ( x) ;
}
template < class T1 , class T2 , class R >
struct pointer_to_binary_function : public binary_function < T1 , T2 , R >
{
protected :
R ( * ptr) ( T1, T2) ;
public :
pointer_to_binary_function ( ) { }
explicit pointer_to_binary_function ( R ( * x) ( T1, T2) ) : ptr ( x) { }
R operator ( ) ( T1 x, T2 y) const
{
return ptr ( x, y) ;
}
} ;
template < class T1 , class T2 , class R >
inline pointer_to_binary_function< T1, T2, R> ptr_fun ( R ( * x) ( T1, T2) )
{
return pointer_to_binary_function < T1, T2, R> ( x) ;
}
template < class T1 , class T2 >
struct mem_fun_t : public unary_function < T1 * , T2 >
{
protected :
explicit mem_fun_t ( T1 ( T2:: * pf) ) : f ( pf) { }
T1 operator ( ) ( T2* p) const
{
return ( p-> * f) ( ) ;
}
private :
T1 ( T2:: * f) ( ) ;
} ;
template < class T1 , class T2 >
struct const_mem_fun_t : public unary_function < const T1 * , T2 >
{
public :
explicit const_mem_fun_t ( T1 ( T2:: * pf) ( ) const ) : f ( pf) { }
T1 operator ( ) ( const T1 * p) const
{
return ( p-> * f) ( ) ;
}
private :
T1 ( T2:: * f) ( ) const ;
} ;
template < class T1 , class T2 >
struct mem_fun_ref_t : public unary_function < T1 , T2 >
{
public :
explicit mem_fun_ref_t ( T1 ( T2:: * pf) ( ) ) : f ( pf) { }
T1 operator ( ) ( T2 & r) const
{
return ( r. f) ( ) ;
}
T1 ( T2:: * f) ( ) ;
} ;
template < class T1 , class T2 >
struct const_mem_fun_ref_t : public unary_function < const T1 , T2 >
{
public :
explicit const_mem_fun_ref_t ( T1 ( T2:: * pf) ( ) const ) : f ( pf) { }
T1 operator ( ) ( const T2& r) const
{
return ( r. * f) ( ) ;
}
private :
T1 ( T2:: * f) ( ) const ;
} ;
template < class S , class T , class Arg >
struct mem_fun1_t : public binary_function < T * , Arg , S > {
public :
explicit mem_fun1_t ( S ( T:: * pf) ( Arg) ) : f ( pf) { }
S operator ( ) ( T * p, Arg x) const { return ( p-> * f) ( x) ; }
private :
S ( T:: * f) ( Arg) ;
} ;
template < class S , class T , class Arg >
struct const_mem_fun1_t : public binary_function < const T * , Arg , S > {
public :
explicit const_mem_fun1_t ( S ( T:: * pf) ( Arg) const ) : f ( pf) { }
S operator ( ) ( const T * p, Arg x) const { return ( p-> * f) ( x) ; }
private :
S ( T:: * f) ( Arg) const ;
} ;
template < class T1 , class T2 , class T3 >
struct mem_fun1_ref_t : binary_function < T2 , T3 , T1 >
{
public :
explicit mem_fun1_ref_t ( T1 ( T2:: * pf) ( T3 x) ) : f ( pf) { }
T1 operator ( ) ( T2 & r, T3 x) const
{
return ( r. * f) ( x) ;
}
private :
T1 ( T2:: * f) ( T1) ;
} ;
template < class T1 , class T2 , class T3 >
struct const_mem_fun1_ref_t : binary_function < T2 , T3 , T1 >
{
public :
explicit const_mem_fun1_ref_t ( T1 ( T2:: * pf) ( T3) const ) : f ( pf) { }
T1 operator ( ) ( const T2& r, T3 x) const
{
return ( r. * f) ( x) ;
}
private :
T1 ( T2:: * f) ( T1) const ;
} ;
template < class T >
struct mem_fun_t < void , T> : public unary_function< T* , void >
{
private :
void ( T:: * f) ( ) ;
public :
explicit mem_fun_t ( void ( T:: * pf) ( ) ) : f ( pf) { }
void operator ( ) ( T * p) const
{
( p-> * f) ( ) ;
}
} ;
template < class T >
struct const_mem_fun_t < void , T> : unary_function< const T* , void >
{
private :
void ( T:: * f) ( ) const ;
public :
explicit const_mem_fun_t ( void ( T:: * pf) ( ) ) : f ( pf) { }
void operator ( ) ( const T* p) const
{
( p-> * f) ( ) ;
}
} ;
template < class T >
struct mem_fun_ref_t < void , T> : public unary_function< T, void >
{
public :
explicit mem_fun_ref_t ( void ( T:: * pf) ( ) ) : f ( pf) { }
void operator ( ) ( T& r) const
{
( r. * f) ( ) ;
}
private :
void ( T:: * f) ( ) ;
} ;
template < class T >
struct const_mem_fun_ref_t < T, void > : public unary_function< T, void >
{
void ( T:: * f) ( ) const ;
public :
explicit const_mem_fun_ref_t ( void ( T:: * pf) ( ) const ) : f ( pf) { }
void operator ( ) ( const T& r) const
{
( r. * f) ( ) ;
}
} ;
template < class T , class Arg >
struct mem_fun1_t < void , T, Arg> : public binary_function< T * , Arg, void > {
public :
explicit mem_fun1_t ( void ( T:: * pf) ( Arg) ) : f ( pf) { }
void operator ( ) ( T * p, Arg x) const { ( p-> * f) ( x) ; }
private :
void ( T:: * f) ( Arg) ;
} ;
template < class T , class Arg >
struct const_mem_fun1_t < void , T, Arg>
: public binary_function< const T * , Arg, void > {
public :
explicit const_mem_fun1_t ( void ( T:: * pf) ( Arg) const ) : f ( pf) { }
void operator ( ) ( const T * p, Arg x) const { ( p-> * f) ( x) ; }
private :
void ( T:: * f) ( Arg) const ;
} ;
template < class T , class R >
struct mem_fun1_ref_t < T , R, void >
: public binary_function< T , R, void >
{
public :
explicit mem_fun1_ref_t ( void ( T:: * pf) ( R) ) : f ( pf) { }
void operator ( ) ( T& r, R x) const
{
( r. * f) ( x) ;
}
private :
void ( T:: * f) ( R) ;
} ;
template < class T , class Arg >
struct const_mem_fun1_ref_t < void , T, Arg>
: public binary_function< T, Arg, void >
{
public :
explicit const_mem_fun1_ref_t ( void ( T:: * pf) ( Arg) const ) : f ( pf) { }
void operator ( ) ( const T & r, Arg x) const { ( r. * f) ( x) ; }
private :
void ( T:: * f) ( Arg) const ;
} ;
template < class T1 , class T2 >
inline mem_fun_t< T1, T2> mem_fun ( T1 ( T2:: * f) ( ) )
{
return mem_fun_t < T1, T2> ( f) ;
}
template < class T1 , class T2 >
inline const_mem_fun_t< T1, T2> mem_fun ( T1 ( T2:: * f) ( ) const )
{
return const_mem_fun_t < T1, T2> ( f) ;
}
template < class T1 , class T2 >
inline mem_fun_ref_t< T1, T2> mem_fun_ref ( T1 ( T2:: * f) ( ) )
{
return mem_fun_ref_t < T1, T2> ( f) ;
}
template < class T1 , class T2 >
inline const_mem_fun_ref_t< T1, T2> mem_fun_ref ( T1 ( T2:: * f) ( ) )
{
return const_mem_fun_ref_t < T1, T2> ( f) ;
}
template < class T1 , class T2 , class T3 >
inline mem_fun1_t< T1, T2, T3> mem_fun ( T1 ( T2:: * f) ( T3) )
{
return mem_fun1_t < T1, T2, T3> ( f) ;
}
template < class T1 , class T2 , class T3 >
inline mem_fun1_ref_t< T1, T2, T3> mem_fun_ref ( T1 ( T2:: * f) ( T3) )
{
return mem_fun1_ref_t < T1, T2, T3> ( f) ;
}
template < class T1 , class T2 , class T3 >
inline const_mem_fun1_ref_t< T1, T2, T3> mem_fun_ref ( T1 ( T2:: * f) ( T3) const )
{
return const_mem_fun1_ref_t < T1, T2, T3> ( f) ;
}
template < class T1 , class T2 , class T3 >
inline mem_fun1_t< T1, T2, T3> mem_fun1 ( T1 ( T2:: * f) ( T3) )
{
return mem_fun1_t < T1, T2, T3> ( f) ;
}
template < class T1 , class T2 , class T3 >
inline const_mem_fun1_t< T1, T2, T3> mem_fun1 ( T1 ( T2:: * f) ( T3) const )
{
return const_mem_fun1_t < T1, T2, T3> ( f) ;
}
template < class T1 , class T2 , class T3 >
inline mem_fun1_ref_t< T1, T2, T3> mem_fun1_ref ( T1 ( T2:: * f) ( T3) )
{
return mem_fun1_ref_t < T1, T2, T3> ( f) ;
}
template < class T1 , class T2 , class T3 >
inline const_mem_fun1_ref_t< T1, T2, T3> mem_fun_ref ( T1 ( T2:: * f) ( T3) )
{
return const_mem_fun1_ref_t < T1, T2, T3> ( f) ;
}
pair类型
template < class T1 , class T2 >
struct pair
{
using first_type = T1;
using second_type = T2;
first_type first;
second_type second;
pair ( ) : first ( first_type ( ) ) , second ( second_type ( ) ) { }
pair ( const first_type& a, const second_type& b) : first ( a) , second ( b) { }
template < class R1 , class R2 >
pair ( const pair< R1, R2> & rhs) : first ( rhs. first) , second ( rhs. second) { }
} ;
template < class T1 , class T2 >
inline bool operator == ( const pair< T1, T2> & lhs, const pair< T1, T2> & rhs)
{
return lhs. first == rhs. first && lhs. second = rhs. second;
}
template < class T1 , class T2 >
inline bool operator != ( const pair< T1, T2> & lhs, const pair< T1, T2> & rhs)
{
return ! ( lhs == rhs) ;
}
template < class T1 , class T2 >
inline bool operator < ( const pair< T1, T2> & lhs, const pair< T1, T2> & rhs)
{
return lhs. first < rhs. first && lhs. second < rhs. second;
}
template < class T1 , class T2 >
inline bool operator > ( const pair< T1, T2> & lhs, const pair< T1, T2> & rhs)
{
return lhs. first > rhs. first && lhs. second > rhs. second;
}
template < class T1 , class T2 >
inline bool operator <= ( const pair< T1, T2> & lhs, const pair< T1, T2> & rhs)
{
return ! ( lhs > rhs) ;
}
template < class T1 , class T2 >
inline bool operator >= ( const pair< T1, T2> & lhs, const pair< T1, T2> & rhs)
{
return ! ( lhs < rhs) ;
}
template < class T1 , class T2 >
inline pair< T1, T2> make_pair ( const T1& first, const T2& second)
{
return pair < T1, T2> ( first, second) ;
}