                               12.  

      ,     , 
 min(), max(), find()  sort(),   (generic)  ,    
  : , , .    
        (    
 6.5), ,      
.  -   
   .

                                  12.1.  

          ,  
        ,    
  .

   ,    ,      ,  
,     ,     :

     :       , 
      .         
    ,   :       ,
      (      C   
    ,       );
          .  
        ,    , 
        ,  ;
           
        ,    .   
         .  ,   ,
     1    0  .

       ,  ,  
    ,   
    ,     
        ,   
.
   ,    ,   :
first   ,  last  ,    .  
,   last,   ;  
,  .  , last   
   .    ,  
,    .

         :    
  ,     -   
,  .

          ,     
  ;     
   .

   ,  find()      int
  :

   #include <algoritm>
   #include <iostream>

   int main()
   {
     int search_value;
     int ia[ 6 ] = { 27, 210, 12, 47, 109, 83 };

     cout << "enter search value: ";
     cin >> search_value;

     int *presult = find( &ia[0], &ia[6], search_value );

     cout << "The value " << search_value
          << ( presult == &ia[6] ? " is not present" : " is present" )
          << endl;

    ,        
 

   int *presult = find( &ia[0], &ia[6], search_value );

   

   int *presult = find( ia, ia+6, search_value );


   #include <algorithm>
   #include <vector>
   #include <iostream>

   int main()
   {
      int search_value;
      int ia[ 6 ] = { 27, 210, 12, 47, 109, 83 };

      vector<int> vec( ia, ia+6 );

      cout << "enter search value: ";
      cin >> search_value;

      vector<int>::iterator presult;
      presult = find( vec.begin(), vec.end(), search_value );

      cout << "The value " << search_value
           << ( presult == vec.end() ? " is not present" : " is present" )
           << endl;


   #include <algorithm>
   #include <list>
   #include <iostream>

   int main()
   {
      int search_value;
      int ia[ 6 ] = { 27, 210, 12, 47, 109, 83 };

      list<int> ilist( ia, ia+6 );

      cout << "enter search value: ";
      cin >> search_value;

      list<int>::iterator presult;
      presult = find( ilist.begin(), ilist.end(), search_value );
      cout << "The value " << search_value
           << ( presult == ilist.end() ? " is not present" : " is present" )
           << endl;


                      12.2.   


   bool less_than( const string & s1, const string & s2 )
   {
       return s1.size() < s1.size();
   }

   void process_vocab( vector<textwords, allocator> *pvec )
   {
      // ...
      //    texts  ,
      //    
      stable_sort( texts.begin(), texts.end(), less_than );
      // ...

       ,    , 
 . less_than()     .  
   (inline) . ,    ,   
   .


   // - -     
   //  operator()
   class LessThan {
   public:
      bool operator()( const string & s1, const string & s2 )
         { return s1.size() < s2.size(); }
   };

   -   ,     (,  
    :  ,  ):

   LessThan lt;
   string st1( "shakespeare" );
   string st2( "marlowe" );

   //  lt.operator()( st1, st2 );
   bool is_shakespeare_less = lt( st1, st2 );


   #include <iostream>

   class GreaterThan {
   public:
      GreaterThan( int size = 6 ) : _size( size ){}
      int size() { return _size; }
      bool operator()( const string & s1 ) { return s1.size() > 6; }
   private:
      int _size;
   };


   int cnt = count_if( texts.begin(), texts.end(), GreaterThan() );

       stable_sort()       
less_than(),     LessThan,    - 
.   -     12.3.)

                                 12.3. -

563

     min()     ,   
 :

        :   
    .

      :   ,   
.

       ,    . 
    ,    ,  
     bool:

   template < typename Type, bool (*Comp)(const Type&, const Type&) >
   const Type& min( const Type *p, int size, Comp comp )
   {
      Type minval = p[ 0 ];
      for ( int ix = 1; ix < size; ++ix )
         if ( Comp( p[ ix ] < minval )) // ? Comp( p[ ix ] , minval )
            minval = p[ ix ];
      return minval;
   }

           ,   
       .
         -
  (     ). -  
,    (operator()).   
   . -,  ,      , 
     -.

    -         . 
   -,        ,  
   ,     . 
   -, -      
,    ,     .
        min() (,   
      ,    ):

   template < typename Type, typename Comp >
   // template < typename Type, bool (*Comp)(const Type&, const Type&) >
   const Type& min( const Type *p, int size, Comp comp )
   {
      Type minval = p[ 0 ];
      for ( int ix = 1; ix < size; ++ix )
         if ( Comp( p[ ix ] < minval )) // ? Comp( p[ ix ] , minval )
           minval = p[ ix ];
      return minval;
   }

       -:

 1.    ,   
    -  ;
 2.     ,  
       (  ) -;
 3.    -   
    .       .


                    12.3.1.  -

    -   ,  
.      ,  
.         :
   
   #include <functional>

   , -,  ,      
plus.   ,     , 
:

   #include <functional>
   plus< int > intAdd;

   int ival1 = 10, ival2 = 20;
   //  int sum = ival1 + ival2;

   int sum = intAdd( ival1, ival2 );

       -      
         . 
   ,    sort()      
      .      
   greater,    :

   vector< string > svec;
   // ...
   sort( svec.begin(), svec.end(), greater<string>() );


                       12.3.2.  -

   template <class FuncObject, class Type>
   Type BinaryFunc( FuncObject fob, const Type &val1, const Type &val2 )

   { return fob( val1, val2 ); }


    : plus<Type>

      plus<string> stringAdd;
      //  string::operator+()
      sres = stringAdd( sval1, sval2 );

      dres = BinaryFunc( plus<double>(), dval1, dval2 );

    : minus<Type>

      minus<int> intSub;
      ires = intSub( ival1, ival2 );

      dres = BinaryFunc( minus<double>(), dval1, dval2 );

    : multiplies<Type>
    : divides<Type>
     : modulus<Type>
      : negate<Type>


                      12.3.3.  -

    : equal_to<Type>

      not_equal_to<complex> complexNotEqual;
      cres = complexNotEqual( cval1, cval2 );

      ires = count_if( svec.begin(), svec.end(), equal_to<string>(), sval1 );

    : not_equal_to<Type>
    : greater<Type>
      : greater_equal<Type>
    : less<Type>
      : less_equal<Type>

                           12.3.4.  -

     : logical_and<Type>

      logical_and<int> intAnd;
      ires = intLess( ival1, ival2 );

      dres = BinaryFunc( logical_and<double>(), dval1, dval2 );

     : logical_or<Type>
     : logical_not<Type>


                     12.3.5.    -

          ,  
    ,    -.
       ,     :

    (binders).  ,   - 
     ,       .

    (negators).  ,    -
      . 

   ,      ,     10, 
  count_if() - less_equal,   
  10.
   ,      ,   10,    
  count_if()  - less_equal,    
  10. ,       - greater,
      10.

         -: bind1st
 bind2nd,  bind1st      
 -,  bind2nd   .
   ,      ,     10, 
     count_if() :

   count_if( vec.begin(), vec.end(), bind2nd( less_equal<int>(), 10 ));

          -: not1 
not2. not1     , 
-,  not2    .
        - less_equal  
:

   count_if( vec.begin(), vec.end(), not1( bind2nd( less_equal<int>(), 10 )));

           ,
     .

 
                           12.3.6.  -

570

         -   
 . , ,  -, , 
     10:

   //    -
   class less_equal_ten {
   public:
      bool operator() ( int val ) { return val <= 10; }
   };

     count_if()    -  
:

   count_if( vec.begin(), vec.end(), less_equal_ten() );

   ,     .  
,  ,    ,  10:

   count_if( vec.begin(), vec.end(), not1(less_equal_then ()));

  ,    ,   
   .        
     ,   
  :

   class less_equal_value {
   public:
      less_equal_value( int val ) : _val( val ) {}
      bool operator() ( int val ) { return val <= _val; }
   private:
      int _val;
   };


   count_if( vec.begin(), vec.end(), less_equal_value( 25 ));

        ,    ,
   :

   template < int _val >
   class less_equal_value {
   public:
      bool operator() ( int val ) { return val <= _val; }
   };

              ,  
 25:

   count_if( vec.begin(), vec.end(), less_equal_value<25>());


                               12.4.    

     ,    vec   const,      
   .     ,    
        . 

++   574

    ,  ,   const-
,  .      :

   vector< type>::const_iterator iter = vec.begin();

    begin()  end()      
      const   .
   ,      .

                               12.4.1.  

       ,    ,   . 
 ,    ?

   int ia[] = { 0, 1, 1, 2, 3, 5, 5, 8 };
   vector< int > ivec( ia, ia+8 ), vres;
   // ...
   //       

   unique_copy( ivec.begin(), ivec.end(), vres.begin() );

     ,   unique_copy()   
      ivec,    
,   vres        .
          unique_copy():  
,    .    ,   ,
   ,       .
    ,    ,  
  ,     :

   back_inserter()      
    push_back()   .  back_inserter()
      . 

     ,  unique_copy()  , :

     // :  unique_copy()    
     // vres.push_back()...
     unique_copy( ivec.begin(), ivec.end(), back_inserter( vres ) );

    front_inserter()      
     push_front()   .  front_inserter() 
       . 

     , ,   vector   push_front(),    
         :

    // , :
    //  vector    push_front()
    //    deque  list
    unique_copy( ivec.begin(), ivec.end(), front_inserter( vres ) );

    inserter()       insert()
       . inserter()   : 
       ,  ,     :

     unique_copy( ivec.begin(), ivec.end(), inserter( vres ), vres.begin() );

    ,     ,    
     ,       ,    
     :

     unique_copy( ivec.begin(), ivec.end(), vector< int >::iterator iter = vres.begin(), iter2 = ivec.begin();
     for ( ; iter2 != ivec.end() ++ iter, ++iter2 )
        vres.insert( iter, *iter2 );


                            12.4.2.  

    begin()  end()   ,  
    ,   .   
 ,       .  
       rbegin() 
rend().       :

   vector< int > vec0;
   const vector< int > vec1;
   vector< int >::reverse_iterator r_iter0 = vec0.rbegin();


                           12.4.3.  

          
         .
 istream_iterator      istream 
    ,  ifstream      
.  ostream_iterator     
ostream      ,  ofstream    
  .        
 

    #include <iterator>

                           12.4.4.  istream_iterator

          istream_iterator  :

   istream_iterator<Type> identifier( istream& );

1.         
  ,   istream_iterator 
      :  difference_type,
      , 
 . ,   12.2   ,
   ,  
   ,  :

   typedef vector<string,allocator>::difference_type diff_type
   istream_iterator< string, diff_type > input_set1( infile1 ), eos;
   istream_iterator< string, diff_type > input_set2( infile2 );


                         12.4.5.  ostream_iterator

       ostream_iterator    
 :


                      12.4.6.   

          
   ,     
.    (InputIterator),  (OutputIterator),
 (ForwardIterator)   
(BidirectionalIterator),      
(RandomAccessIterators).      
:

                            12.5.  

         (,  ,
   )    ,  
first  last,       
,     .  ,  
(       ) 
 :

   [ first, last )

       ,      first   
 last,  . 

   first == last

 ,   .

                                 12.5.1.  

     

                          12.5.2.    

       

                            12.5.3.    

       

                           12.5.4.  

   next_permutation(), prev_permutation()

                           12.5.5.  

   accumulate(), partial_sum(), inner_product(), adjacent_difference()

                  12.5.6.    

            
,     .

   fill(), fill_n(), for_each(), generate(),generate_n(), transform()

                          12.5.7.  

            

                       12.5.8.    

        -  
  .

                     12.5.9.    

    (heap)     ,   .
     ,   
             .

   make_heap(), pop_heap(), push_heap(), sort_heap()


             12.6.     

     (  )  
      .     
  ,  , ,  sort() 
partition().       , 
      ,   
 .

                          12.6.1.  list_merge()

                          12.6.2.  list::remove()

                           12.6.3.  list::remove_if()

                           12.6.4.  list::reverse()

                             12.6.5.  list::sort()

                             12.6.6.  list::splice()

                            12.6.7.  list::unique()

