                                                      ++   551
                                        12

                            12.  

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

                                  12.1.  

          ,  
        ,    
  .   find().   
, ,   ,     :

  1.     .
  2.     ,      .
  3.         2, 
               .
  4.         ,   
     , ,    .

   ,    ,      ,  
,     ,     :

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

                                                                ++   552

       ,  ,  
    ,   
    ,     
        ,   
. ,    ,   :
first   ,  last  ,    .  
,   last,   ;  
,  .  , last   
   .    ,  
,    .
         :    
  ,     -   
,  . (-    12.3.)
, ,    find(),   
       :

   template < class ForwardIterator, class Type >
   ForwardIterator
   find( ForwardIterator first, ForwardIterator last, Type value )
   {
      for ( ; first != last; ++first )
         if ( value == *first )
            return first;
      return last;
   }

   ForwardIterator ( )      
,    .    
  . (      12.4.)
          ,     
  ;     
   .     ,   ,
      .     
        ,  
. ,  find()      int
  :

                                                              ++   553

   #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;
   }

        &ia[6] (   
 ),    ,    
.
    ,        
 

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



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

          ,    
   . ,    
find()       (,  
   ):

   //     ia[1]  ia[2]
   int *presult = find( &ia[1], &ia[3], search_value );

         vector   find():

                                                          ++   554

   #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;
   }

find()     :

   #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.4
    .    
    12.5,      
   .       , 
   .)

                                      12.1

       ,      
   . ,    
  ,   ,    

                                                              ++   555

.     ?    
     ? 
,      , , 
 ,     ?

                 12.2.   

   ,         ,  
     .     , 
   ,       (.
 6.7)     :

  1.    .
  2.     .
  3.     .
  4.   .
  5.  ,     .
  6.   ,      (, 
          ,   ,   ).
  7.     (,  and (), if (), or
     (), but ()  ..).
  8.   .

     ,    .      
     .
          .   
 , ,     :

   #include <vector>
   #include <string>

   typedef vector<string, allocator> textwords;
   void process_vocab( vector<textwords, allocator> *pvec )
   {
      if ( ! pvec ) {
         //   
         return;
      }

      // ...
   }

      ,     .  
    copy() (   
   algorithm  iterator):

                                                         ++   556

   #include <algorithm>
   #include <iterator>

   void process_vocab( vector<textwords, allocator> *pvec )
   {
      // ...
      vector< string > texts;

      vector<textwords, allocator>::iterator iter = pvec->begin();
      for ( ; iter != pvec->end(); ++iter )
      copy( (*iter).begin(), (*iter).end(), back_inserter( texts ));

      // ...
   }

       copy()  , 
   .     ,
  ,    . back_inserter 
 ;       , 
   . (      
12.4.).
    unique()    ,  .  
 01123211,    012321,   0123.  
 ,      
 sort();    01111223  0123. ( 
   01231223.)
   unique()    .     
    ,   .   
     01231223;  1223  , 
,  . unique()  ,   
 .  ,      erase() 
  . (    
 erase(),    unique()     
  .)    :

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

      //  
      vector<string, allocator>::iterator it;
      it = unique( texts.begin(), texts.end() );
      texts.erase( it, texts.end() );

      // ...
   }

        texts,   
 ,   sort(),    unique():

                                                       ++   557

   a a a a alice alive almost
   alternately ancient and and and and and and
and as asks at at beautiful becomes bird
bird blows blue bounded but by calling coat
daddy daddy daddy dark darkened darkening distant each
either emma eternity falls fear fiery fiery flight
flowing for grow hair hair has he heaven,
held her her her her him him home
houses i immeasurable immensity in in in in
inexpressibly is is is it it it its
journeying lands leave leave life like long looks
magical mean more night, no not not not
now now of of on one one one
passion puts quite red rises row same says
she she shush shyly sight sky so so
star star still stone such tell tells tells
that that the the the the the the
the there there thing through time to to
   to to trees unravel untamed wanting watch what
   when wind with with you you you you
   your your

     unique()    erase()  texts 
 :

   a alice alive almost alternately ancient
   and as asks at beautiful becomes bird blows
   blue bounded but by calling coat daddy dark
darkened darkening distant each either emma eternity falls
fear fiery flight flowing for grow hair has
he heaven, held her him home houses i
immeasurable immensity in inexpressibly is it its journeying
lands leave life like long looks magical mean
more night, no not now of on one
passion puts quite red rises row same says
she shush shyly sight sky so star still
stone such tell tells that the there thing
   through time to trees unravel untamed wanting watch
   what when wind with you your

          .    
  sort(),   stable_sort(),   
  .       
 .        
 .     :

   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 );
      // ...
   }

                                                              ++   558

       ,    , 
 . less_than()     .  
   (inline) . ,    ,   
   . ,   , 
-:

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

   -   ,      operator().  
     ,     .
     -   . 

   operator()
 
 ,     .   

   ( const string & s1, const string & s2 )

    .     
   less_than(),  , ,  
 less_than  operator(),  .
   -   ,     (,  
    :  ,  ):

   LessThan lt;

            
  ,   . :

   string st1( "shakespeare" );
   string st2( "marlowe" );
   
   //  lt.operator()( st1, st2 );
   bool is_shakespeare_less = lt( st1, st2 );

       process_vocab(),   
stable_sort()   - LessThan():

                                                         ++   559

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

   // ...
   }

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

   a i
   as at by he in is it no
of on so to and but for has
her him its not now one red row
she sky the you asks bird blue coat
dark each emma fear grow hair held home
life like long mean more puts same says
star such tell that time what when wind
with your alice alive blows daddy falls fiery
lands leave looks quite rises shush shyly sight
still stone tells there thing trees watch almost
either flight houses night, ancient becomes bounded calling
   distant flowing heaven, magical passion through unravel untamed
   wanting darkened eternity beautiful darkening immensity journeying alternately
   immeasurable inexpressibly

     ,     ,   
  count_if()    -  GreaterThan. 
  ,      ,  
 .          
  (    6):

   #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;
   };

      :

                                                          ++   560

   void process_vocab( vector<textwords, allocator> *pvec )
   {
      // ...
      //   ,    6

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

      cout << "Number of words greater than length six are "
           << cnt << endl;

      // ...
   }

        :

   Number of words greater than length six are 22

    remove()    unique():     
,      ,    (  
   ),  ,    (   
).          ,
    :

   void process_vocab( vector<textwords, allocator> *pvec )
   {
      // ...
      static string rw[] = { "and", "if", "or", "but", "the" };
      vector< string > remove_words( rw, rw+5 );

      vector< string >::iterator it2 = remove_words.begin();
      for ( ; it2 != remove_words.end(); ++it2 ) {
         //      count()
         int cnt = count( texts.begin(), texts.end(), *it2 );
         cout << cnt << " instances removed: "
              << (*it2) << endl;

         texts.erase(
            remove(texts.begin(),texts.end(),*it2 ),
                   texts.end()
         );
      }

      // ...
   }

     remove():

   1 instances removed: and
   0 instances removed: if
   0 instances removed: or
   1 instances removed: but
   1 instances removed: the

                                                         ++   561

        .     
   , ,      
,     .   
  for_each()     . for_each()
     -    
 ,   .    -
PrintElem      :

   class PrintElem {
   public:
      PrintElem( int lineLen = 8 )
         : _line_length( lineLen ), _cnt( 0 )
      {}

      void operator()( const string &elem )
      {
         ++_cnt;
         if ( _cnt % _line_length == 0 )
         { cout << '\n'; }
           cout << elem << " ";
         }

   private:
      int _line_length;
      int _cnt;
   };

   void process_vocab( vector<textwords, allocator> *pvec )
   {
      // ...
      for_each( texts.begin(), texts.end(), PrintElem() );
   }

     .    ,    
      . 
         main()  
 (    ,  
    12.4).      ,
     C++.  ,  
      count()  count_if(),
   ,      
 .  ,  iostream  
  ,      iostream.h.

                                                            ++   562

   #include <vector>
   #include <string>
   #include <algorithm>
   #include <iterator>

   //     <iostream>

   #include <iostream.h>

   class GreaterThan {
   public:
      GreaterThan( int size = 6 ) : _size( sz ){}
      int size() { return _size; }

      bool operator()(const string &s1)
      { return s1.size() > _size; }
   private:
      int _size;
   };

   class PrintElem {
   public:

      PrintElem( int lineLen = 8 )
         : _line_length( lineLen ), _cnt( 0 )
      {}

      void operator()( const string &elem )
      {
          ++_cnt;
          if ( _cnt % _line_length == 0 )
             { cout << '\n'; }
          cout << elem << " ";
      }
   private:
      int _line_length;
      int _cnt;
                                                          ++   563

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

   typedef vector<string, allocator> textwords;

   void process_vocab( vector<textwords, allocator> *pvec )
   {
      if ( ! pvec ) {
         //   
         return;
      }

      vector< string, allocator > texts;

      vector<textwords, allocator>::iterator iter;
      for ( iter = pvec->begin() ; iter != pvec->end(); ++iter )
         copy( (*iter).begin(), (*iter).end(),
         back_inserter( texts ));

      //   texts
      sort( texts.begin(), texts.end() );

      //  ,  
      for_each( texts.begin(), texts.end(), PrintElem() );

      cout << "\n\n"; //    

      //  
      vector<string, allocator>::iterator it;
      it = unique( texts.begin(), texts.end() );
      texts.erase( it, texts.end() );

      // ,  
      for_each( texts.begin(), texts.end(), PrintElem() );
      cout << "\n\n";

      //  
      // stable_sort     
      stable_sort( texts.begin(), texts.end(), LessThan() );
      for_each( texts.begin(), texts.end(), PrintElem() );

      cout << "\n\n";

      //   ,    6
      int cnt = 0;

      //   count -    
      count_if( texts.begin(), texts.end(), GreaterThan(), cnt );

      cout << "Number of words greater than length six are "
           << cnt << endl;

      static string rw[] = { "and", "if", "or", "but", "the" };
      vector<string,allocator> remove_words( rw, rw+5 );

      vector<string, allocator>::iterator it2 = remove_words.begin();

      for ( ; it2 != remove_words.end(); ++it2 )
      {
         int cnt = 0;

         //   count -    
         count( texts.begin(), texts.end(), *it2, cnt );

         cout << cnt << " instances removed: "
              << (*it2) << endl;

         texts.erase(
            remove(texts.begin(),texts.end(),*it2),
            texts.end() );
      }
      cout << "\n\n";
      for each( texts.begin(), texts.end(), PrintElem() );

                                                          ++   564
   }

                                    12.2

        , ,     . 
     .  ,  
      ,     
     count().    
.          
     ,       sort()
  -,  ,    ,   .
(      ,   
       ,   
   .)

                                  12.3

            
.     1 ,   
    2 ,    3 .   
 12.2 ,      .
  count_if()      
  .     .

                               12.3. -

     min()     ,   
 :

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

           min(),
     .  
  ,      min()     .
        :   
    . ,   Image   
   ,         
      .  
 min()       :

   error: invalid types applied to the < operator: Image < Image
   (:  <    : Image < Image)

                                                            ++   565

      :   ,   
. ,      ,    
   ,   ,     
    .
       ,    . 
    ,    ,  
     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 ))
            minval = p[ ix ];
      return minval;
   }

             
      ,   
Image,         
.        
,        
 .
         -
  (     ). -  
,    (operator()).   
   . -,  , 
    ,     
-. ,     - AddImages,
   ,      
 ,        :

   AddImages AI;

    -   ,   
,        Image:

   Image im1("foreground.tiff"), im2("background.tiff");
   // ...
   //  Image AddImages::operator()(const Image1&, const Image2&);
   Image new_image = AI (im1, im2 );

    -         . -
,        ,  
   ,    
. -, -   

                                                            ++   566

  ,    ,  
  .
        min() (,   
      ,    ):

   template < typename Type,
              typename Comp >
   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 ))
            minval = p[ ix ];
      return minval;
   }

    ,       :
   ( ) ,   
    -.
       -:

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

           -.

                      12.3.1.  -

    -   ,  
.      ,  
.         :

   #include <functional>

   , -,  ,      
plus.   ,     , 
:

   #include <functional>
   plus< int > intAdd;

            
intAdd   ,     AddImage   :

                                                             ++   567

   int ival1 = 10, ival2 = 20;

   //  int sum = ival1 + ival2;
   int sum = intAdd( ival1, ival2 );

      plus   ,  
    int.     -
         
    . ,  
 sort()        
   .      
   greater,    :

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

    -       
: ,   .    
    ,      ,
 .     , 
    (     
15):

   class Int {
   public:
      Int( int ival = 0 ) : _val( ival ) {}

      int operator-() { return -_val; }
      int operator%(int ival) { return -_val % ival; }

      bool operator<(int ival) { return -_val < ival; }
      bool operator!() { return -_val == 0; }
   private:
      int _val;
   };

   vector< string > svec;
   string sval1, sval2, sres;
   complex cval1, cval2, cres;
   int ival1, ival2, ires;
   Int Ival1, Ival2, Ires;
   double dval1, dval2, dres;

    ,     ,   
 -:

                                                         ++   568

   template <class FuncObject, class Type>
   Type UnaryFunc( FuncObject fob, const Type &val )
   { return fob( val ); }

   template <class FuncObject, class Type>
   Type BinaryFunc( FuncObject fob,
                    const Type &val1, const Type &val2 )
   { return fob( val1, val2 ); }

                      12.3.2.  -

     -  
, , , ,    
   .     ,
   Type.    , 
  ,      .

   : 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>

     multiplies<complex> complexMultiplies;
     cres = complexMultiplies( cval1, cval2 );
    dres = BinaryFunc( multiplies<double>(), dval1, dval2 );

   : divides<Type>

     divides<int> intDivides;
     ires = intDivides( ival1, ival2 );
     dres = BinaryFunc( divides<double>(), dval1, dval2 );

    : modulus<Type>

                                                        ++   569

     modulus<Int> IntModulus;
     Ires = IntModulus( Ival1, Ival2 );
     ires = BinaryFunc( modulus<int>(), ival1, ival2 );

     : negate<Type>

     negate<int> intNegate;
     ires = intNegate( ires );
     Ires = UnaryFunc( negate<Int>(), Ival1 );

                      12.3.3.  -

    -   , ,
,   , ,   .

   : equal_to<Type>

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

   : not_equal_to<Type>

     not_equal_to<complex> complexNotEqual;
     cres = complexNotEqual( cval1, cval2 );
     ires = count_if( svec.begin(), svec.end(),
                      not_equal_to<string>(), sval1 );

    : greater<Type>

     greater<int> intGreater;
     ires = intGreater( ival1, ival2 );
     ires = count_if( svec.begin(), svec.end(),
                      greater<string>(), sval1 );

     : greater_equal<Type>

     greater_equal<double> doubleGreaterEqual;
     dres = doubleGreaterEqual( dval1, dval2 );
     ires = count_if( svec.begin(), svec.end(),

                                                        ++   570

                      greater_equal <string>(), sval1 );

   : less<Type>

     less<Int> IntLess;
     Ires = IntLess( Ival1, Ival2 );
     ires = count_if( svec.begin(), svec.end(),
                      less<string>(), sval1 );

     : less_equal<Type>

       less_equal<int> intLessEqual;
       ires = intLessEqual( ival1, ival2 );
       ires = count_if( svec.begin(), svec.end(),
                        less_equal<string>(), sval1 );

                            12.3.4.  -

    -    Ȕ ( true,
    true,    &&, c  
Type),  Ȕ ( true,       
true,    ||, c   Type)   Ŕ
( true,    false,    !, c
  Type)

    : logical_and<Type>

     logical_and<int> intAnd;
     ires = intLess( ival1, ival2 );
     dres = BinaryFunc( logical_and<double>(), dval1, dval2 );

    : logical_or<Type>

     logical_or<int> intSub;
     ires = intSub( ival1, ival2 );
     dres = BinaryFunc( logical_or<double>(), dval1, dval2 );

    : logical_not<Type>

     logical_not<Int> IntNot;
     ires = IntNot( Ival1, Ival2 );
     dres = UnaryFunc( logical_or<double>(), dval1 );

                                                             ++   571

                     12.3.5.    -

          ,  
    ,    -.
    ,     :

    (binders).  ,   - 
     ,       . ,
         ,     10, 
      count_if() - less_equal,   
      10.     ,   ;
    (negators).  ,    -
      . ,     
    ,   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.  -

        12.2     
-.          
   -. (  13  
 ;   15   .)

                                                            ++   572

         -   
 . , ,  -, , 
     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;
   };

    -      .
,      ,  
 25:

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

        ,    ,
   :

                                                                  ++   573

   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

     -  ,  -
   :

   (a)   ,    1024.
   (b)   ,   "pooh".
   (c)     2.

                                    12.5

 -      . 
     .    
      . ,    
  .

                            12.4.    

        .    ,
?

   //      
   template < typename type >
   int
   count( const vector< type > &vec, type value )
   {
      int count = 0;

      vector< type >::iterator iter = vec.begin();
      while ( iter != vec.end() )
         if ( *iter == value )
            ++count;

      return count;
   }

     ,    vec   const,      
   .     ,    
        . 

                                                                  ++   574

    ,  ,   const-
,  .      :

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

   ,   const-     ,
   ,  const-   
.       , 
 const-   .
    begin()  end()      
      const   . 
   :

   vector< int > vec0;
   const vector< int > vec1;

    begin()  end()  vec0   ,  
vec1   :

   vector< int >::iterator iter0 = vec0.begin();
   vector< int >::const_iterator iter1 = vec1.begin();

   ,      .
:

   // :    
   vector< int >::const_iterator iter2 = vec0.begin();

                          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() );

                                                            ++   575

     ,   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() );

   ,     ,    
    ,       ,    
    :

     vector< int >::iterator iter = vres.begin(),
        iter2 = ivec.begin();

     for ( ; iter2 != ivec.end() ++ iter, ++iter2 )
        vres.insert( iter, *iter2 );

                                                              ++   576

                         12.4.2.  

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

   vector< int > vec0;
   const vector< int > vec1;

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

       ,  .    
      .   
 ++      ,      
. ,        :

   //        
   vector< type >::reverse_iterator r_iter;

   for ( r_iter = vec0.rbegin(); // r_iter    
      r_iter != vec0.rend(); //      
      r_iter++ ) //    

      { /* ... */ }

           ,
         
. ,         
sort()   :

   //     
   sort( vec0.begin(), vec0.end() );
   //     
   sort( vec0.rbegin(), vec0.rend() );

                         12.4.3.  

          
         .
 istream_iterator      istream 
    ,  ifstream      
.  ostream_iterator     
ostream      ,  ofstream    
  .        
 

                                                          ++   577

   #include <iterator>

             
      ,   
        
unique_copy():

   #include <iostream>
   #include <iterator>
   #include <algorithm>
   #include <vector>
   #include <functional>

   /*
    * :
    * 23 109 45 89 6 34 12 90 34 23 56 23 8 89 23
    *
    * :
    * 109 90 89 56 45 34 23 12 8 6
    */

   int main()
   {
      istream_iterator< int > input( cin );
      istream_iterator< int > end_of_stream;

      vector<int> vec;
      copy ( input, end_of_stream, inserter( vec, vec.begin() ));

      sort( vec.begin(), vec.end(), greater<int>() );

      ostream_iterator< int > output( cout, " " );
      unique_copy( vec.begin(), vec.end(), output );
   }

                      12.4.4.  istream_iterator

          istream_iterator  :

   istream_iterator<Type> identifier( istream& );1.

   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 );

            C++,  
        :

      istream_iterator< string > input_set1( infile1 ), eos;
      istream_iterator< string > input_set2( infile2 );
          
                                                       ++   578

 Type        ,  
  .       
istream,  cin,        
  ifstream:

   #include <iterator>
   #include <fstream>
   #include <string>
   #include <complex>

   //     complex
   //   
   istream_iterator< complex > is_complex( cin );

   //      
   ifstream infile( "C++Primer" );
   istream_iterator< string > is_string( infile );

           istream_iterator
     ,    
operator>>().        , 
  ,       .
   istream_iterator,  
istream,  , ,  is_string.     
      istream_iterator:

   //   end_of_stream,    
   //     
   istream_iterator< string > end_of_stream

   vector<string> text;

   // :   
   copy( is_string, end_of_stream,
       inserter( text, text.begin() ));


                         12.4.5.  ostream_iterator

       ostream_iterator    
 :

                                                               ++   579

   ostream_iterator<Type> identifier( ostream& )
   ostream_iterator<Type> identifier( ostream&, char * delimiter )

 Type        ,  
   (operator<<).    delimiter  
,   C- ,      
.      ,  
   ( ,    ).  
 ostream     ostream,  cout, 
       ,  ofstream:

   #include <iterator>
   #include <fstream>
   #include <string>
   #include <complex>

   //     complex
   //   ,   
   ostream_iterator< complex > os_complex( cin, " " );

   //      
   ofstream outfile( "dictionary" );
   ostream_iterator< string > os_string( outfile, "\n" );

              
        copy():

   #include <iterator>
   #include <algorithm>
   #include <iostream>

   int main()
   {
      copy( istream_iterator< int >( cin ),
            istream_iterator< int >(),
            ostream_iterator< int >( cout, " " ));
   }

      ,    
      ,     copy() 
   ostream_iterator:

                                                          ++   580

   #include <string>
   #include <algorithm>
   #include <fstream>
   #include <iterator>

   main()
   {
      string file_name;

      cout << "please enter a file to open: ";
      cin >> file_name;

      if ( file_name.empty() || !cin ) {
          cerr << "unable to read file name\n"; return -1;
      }

      ifstream infile( file_name.c_str());
      if ( !infile ) {
         cerr << "unable to open " << file_name << endl;
         return -2;
      }

      istream_iterator< string > ins( infile ), eos;
      ostream_iterator< string > outs( cout, " " );
      copy( ins, eos, outs );
   }

                            12.4.6.   

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

           , 
         .   
       (,  
     ,       
    ,     ):  
        ,    
          ( ++), 
         (*).   
    ,  ,  find(), accumulate()  equal(). 
    ,    ,    
     ,    3, 4  5;
          
      .  ,    
      ,       .
            
    (, copy())    ,     .

                                                            ++   581

     ,    ,    
      ,    3, 4  5;
            ,
          (    
      ).    , 
       ,  adjacent_find(),
    swap_range()  replace(). ,  ,  
     ,       
    ;
          ,  
         .   ,
        ,  place_merge(),
    next_permutation()  reverse();
      ,   ,
      ,    
         .   
      ,  binary_search(), sort_heap()  nthelement().

                               12.6

   ,    .    
 ?

  (a) const vector<string> file_names( sa, sa+6 );
      vector<string>::iterator it = file_names.begin()+2;

  (b) const vector<int> ivec;
      fill( ivec.begin(), ivec.end(), ival );

  (c) sort( ivec.begin(), ivec.end() );

  (d) list<int> ilist( ia, ia+6 );
      binary_search( ilist.begin(), ilist.end() );

  (e) sort( ivec1.begin(), ivec3.end() );

                                  12.7

    ,       
      istream_iterator.  
     ostream_iterator,   .
        ,    
    .

                           12.5.  

         (,  ,
   )    ,  
first  last,       
,     .  ,  

                                                            ++   582

(       ) 
 :

   //  :      ,
   //  
   [ first, last )

       ,      first   
 last,  . 

   first == last

 ,   .
        :     first 
   ,     last.
        ;  
,    ,    
   .
          
 (.  12.4). ,   find(), 
       ,  
,        ,  
   .      
.  ,  ,      
,     ,    
   ,   -,   .
        :   
 ,     -    , 
   . , unique() 
        ,
     .      
       ,    
-,    ,   .
    ,   . , 
   ,   _if,  find_if(). , 
 replace(),      , 
replace_if(),   -    .
   ,  ,    ,  
 :      ,   
  ,      . , 
 replace()  replace_copy() (    
  _copy).     ,  ,
  .  ,     sort().    , 
 ,       .
           
 

   #include <algorithm>

                                                           ++   583

           adjacent_differences(),
accumulate(), inner_product()  partial_sum()    

   #include <numeric>

            
 (  ).     
 ,      .

                            12.5.1.  

         
   .   equal_range(), lower_bound() 
upper_bound()       .  , 
      ,    .

   adjacent_find(), binary_search(), count(),count_if(), equal_range(),
   find(), find_end(), find_first_of(), find_if(), lower_bound(),
   upper_bound(), search(), search_n()

                   12.5.2.    

          
  .  (partition)    
   :     
. ,      /   
  ,        . 
(stable)       
       . ,  
:

   { "pshew", "honey", "Tigger", "Pooh" }

    /     
 ,       
 :

   { "Tigger", "Pooh", "pshew", "honey" }

          
. (,        
 , ,   (set)   (map).)

   inplace_merge(), merge(), nth_element(), partial_sort(),
   partial_sort_copy(), partition(), random_shuffle(), reverse(),
   reverse_copy(), rotate(), rotate_copy(), sort(), stable_sort(),
   stable_partition()

                                                       ++   584

              12.5.3.    

          
       . unique() 
  . iter_swap()   ,
  ,     .

   copy(), copy_backwards(), iter_swap(), remove(), remove_copy(),
   remove_if(),remove_if_copy(), replace(), replace_copy(),
   replace_if(), replace_copy_if(), swap(), swap_range(), unique(),
   unique_copy()

                            12.5.4.  

       : {a,b,c}.    
 : abc, acb, bac, bca, cab  cba, 
    .  , abc   
,      . 
  acb,       a   
.  ,   b, 
,    .  bac  bca   bac,  
 ac  ,  ca.    bca,
  ,      bac,    cab. 
 abc  ,   cba  .

   next_permutation(), prev_permutation()

                           12.5.5.  

          .  
     <numeric>.

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

               12.5.6.    

            
,     .

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

                    12.5.7.  

            
( min()  max()   ). 

                                                         ++   585

lexicographical_compare()   () 
(.     ).

   equal(), includes(), lexicographical_compare(), max(), max_element(),
   min(), min_element(), mismatch()

                   12.5.8.    

        -  
  .    
 ,     , 
   ,       
,    . ,    
  ,    ,
  .

   set_union(), set_intersection(), set_difference(),
   set_symmetric_difference()

                  12.5.9.    

    (heap)     ,   .
     ,   
             .

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

                 12.6.    
                               

     (  )  
      .     
  ,  , ,  sort() 
partition().       , 
      ,   
 .
    list ()     :   ,
  ,   -     
 .        
         , 
    . ,  :

   vector<string>::iterator vec_iter = vec.begin() + 7;

                                                           ++   586

         vec_iter   
,  

   // :    
   //   
   list<string>::iterator list_iter = slist.begin() + 7;

,         . 
     ,    .
        ,   merge(),
remove(), reverse(), sort()  unique()      ,
          .  
        
-,    splice():

   list::merge()    
   list::remove()     
   list::remove_if() ,   
   list::reverse()      
   list::sort()   
   list::splice()       
   list::unique()        
    


                          12.6.1.  list_merge()

   void list::merge( list rhs );
   template <class Compare>
   void list::merge( list rhs, Compare comp );

           
,      ,    
  . (,    rhs  
,    - merge();    
rhs  .) :

                                                              ++   587

   int array1[ 10 ] = { 34, 0, 8, 3, 1, 13, 2, 5, 21, 1 };
   int array2[ 5 ] = { 377, 89, 233, 55, 144 };

   list< int > ilist1( array1, array1 + 10 );
   list< int > ilist2( array2, array2 + 5 );

   //   ,     
   ilist1.sort(); ilist2.sort();
   ilist1.merge( ilist2 );

      merge()  ilist2 ,  ilist1   15
    .

                         12.6.2.  list::remove()

    remove()      :

   void list::remove( const elemType &value );
   ilist1.remove( 1 );

                    12.6.3.  list::remove_if()

   template < class Predicate >
   void list::remove_if( Predicate pred );

    remove_if()   ,    
, ..  pred  true. :

   class Even {
   public:
      bool operator()( int elem ) { return ! (elem % 2 ); }
   };
   ilist1.remove_if( Even() );

     ,    merge().

                   12.6.4.  list::reverse()

   void list::reverse();

                                                             ++   588

    reverse()      
:

   ilist1.reverse();


                          12.6.5.  list::sort()

   void list::sort();
   template <class Compare>
   void list::sort( Compare comp );

     sort()       
 ,     .   
      . ,

   list1.sort();

 list1  , 

   list1.sort( greater<int>() );

 list1  ,   .

                       12.6.6.  list::splice()

   void list::splice( iterator pos, list rhs );
   void list::splice( iterator pos, list rhs, iterator ix );
   void list::splice( iterator pos, list rhs,
                      iterator first, iterator last );

    splice()   :   ,   
     .     ,
   ,    
  .    :

   int array[ 10 ] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 };
   list< int > ilist1( array, array + 10 );
   list< int > ilist2( array, array + 2 ); //  0, 1

    splice()    ilist1  ilist2.
 ilist2   0, 1  0,    ilist1  0  .

                                                              ++   589

   // ilist2.end()   ,    
   //     
   // ilist1   ,    
   // ilist1.begin()     
   ilis2.splice( ilist2.end(), ilist1, ilist1.begin() );

       splice()   ,
   :

   list< int >::iterator first, last;
   first = ilist1.find( 2 );
   last = ilist1.find( 13 );
   ilist2.splice( ilist2.begin(), ilist1, first, last );

       2, 3, 5  8   ilist1    
ilist2.  ilist1    1, 1, 13, 21  34.   
 ilist2      splice():

   list< int >::iterator pos = ilist2.find( 5 );
   ilist2.splice( pos, ilist1 );

   ,  ilist1 .       
ilist2,  ,    5.

                         12.6.7.  list::unique()

   void list::unique();
   template <class BinaryPredicate>
   void list::unique( BinaryPredicate pred );

    unique()   .    
  ,     .
,    {0,2,4,6,4,2,0},    unique() 
  ,      .    
 ,   {0,0,2,2,4,4,6},    unique(),  
   {0,2,4,6}.

   ilist.unique();

     unique()    . ,

                                                          ++   590

   class EvenPair {
   public:
      bool operator()( int val1, val2 )
      { return ! (val2 % val1 ); }
   };
   ilist.unique( EvenPair() );

  ,        .
    ,   ,   
     .   ,
,  find(), transform(), for_each()  ..,     
,      (  ,   
   ).

                                 12.8

       12.2,    .
