                                                             ++   39

                                 2.   ++

            ++   .
      ,      
.   ,   ,    
   Array,      . 
    ,      Array.
       Array   vector  
 ++,   .   
      ++,  ,   
 .

                          2.1.    

        1, ++     
      ,    :

   //    ival
   // ival   1024
   int ival = 1024;

   //      dval
   // dval   3.14159
   double dval = 3.14159;

   //      fval
   // fval   3.14159
   float fval = 3.14159;

            
:     , , ,   ..

   int ival2 = ival1 + 4096; // 
   int ival3 = ival2 - ival; // 
   dval = fval * ival; // 
   ival = ival3 / 2; // 
   bool result = ival2 == ival3; //   
   result = ival2 + ival != ival3; //   
   result = fval + ival2 < dval; //   
   result = ival > ival2; //   

          ++ 
    , ,     . (
   vector      2.7.)

                                                                ++   40

            
        . (
   2.2.)
          . ,


   0 1 1 2 3 5 8 13 21

   9   . (
  ,        
.)
            , 
    ++:

   int fibon[9] = { 0, 1, 1, 2, 3, 5, 8, 13, 21 };

    fibon    .     int,  ()
  9.     0,   21.    
  ()  ,       
  .  ,      
 :

   int first_elem = fibon[1];

       :  ++ (   )     0,
    1       , 
   0. ,     
,       :

   fibon[0]; //  
   fibon[1]; //  
   ...
   fibon[8]; //  
   fibon[9]; // ... 

      fibon    0  8.   
 1-9       
  ++.
          .  
,         0  9 
     :

   int main()
   {
      int ia[10];
      int index;

                                                              ++   41



      for (index=0; index<10; ++index)
         // ia[0] = 0, ia[1] = 1  ..
         ia[index] = index;

      for (index=9; index>=0; --index)
         cout << ia[index] << " ";
         cout << endl;
   }

       10 .    for 
       for.  
   index.       :

   index = 0;

    :

   index < 10;

    .      
 .      true,   
;     false,  .  
     ,    index  10. 
        ,
  .     

   ia[index] = index;

      

   ++index

    ,    .    
  index  .        

   index = index + 1

 ++      (  ) 
.     .  
   ,      .
     for      .   
  ,     index   9  0. (
 for    5.)
     ,   ++      ,  
.         
. ++    ,    
  , , ,      
    ,    ,   ,
,    .       
,    :

                                                            ++   42

   int array0[10]; array1[10];
   ...
   array0 = array1; // 

            :

   for (int index=0; index<10; ++index)
      array0[index] = array1[index];

       .       , 
      .   
   ,       .
 ,       ++     
-  .    
     .

                                 2.1

     ,       
?     ,    ?

                                 2.2

        ?

                 2.2.     

       - ,   
         ++.    
-  ,        
  .
    ++          , 
     ,     
.           
.    ,    
   ,     ,   
       .  ,   
        : 
    . ,      
   ,     .
             .
,   ival

   int ival = 1024;

     ,   
  int,      ival     1024.
     ,   .
     ival   :   , 1024 
 ,     ,    .  
      .   :

   int ival2 = ival + 1;

                                                              ++   43

   ,    ival:    1 
  ival2   , 1025.   
  ,    ?
   ++    ,     
.   ,    ival,  
:

   int *pint; //     int

        ,   &. 
   .     pint
  ival:

   pint = &ival; // pint    ival

        ,    pint (ival  
),   ,    .
    *.       
ival,   :

   *pint = *pint + 1; //   ival

          ,  

   ival = ival + 1; //   ival

         :    
  ival     .   
   ,       . 
        
 .
           :

       ,  
        ,    .
         ,    
     ,   ;
          
     .       .
            
       .    , 
        .   
        new  delete.

    new   .       
 :

   int *pint = new int(1024);

                                                                  ++   44

     new       int, 
  1024     .    
  pint.      
    , ..  
  .
      new      , 
   :

   int *pia = new int[4];

              int. 
,    new    
.
      ,     new  
,       .  pint,  pia  
,  pint      int,  pia   
      int.
        ,     
   .      delete, , 
 new,         :

   //   
   delete pint;
   //  
   delete[] pia;

    ,      ?  
 ,   ,    
,       .    
  .       -
  (, ,     ).  
  ,   ,   .
           ,
,   ,   .   8.4  
    .       
,    Array,     
 ,      .

                                     2.3

       :

   (a) int ival = 1024;
   (b) int *pi = &ival;
   (c) int *pi2 = new int(1024);
   (d) int *pi3 = new int[1024];

                                     2.4

                                                                 ++   45

       ?     ? (, 
   ([])     pia.  
     3.9.2.)

   int *pi = new int(10);
   int *pia = new int[10];
   while ( *pi < 10 ) {
      pia[*pi] = *pi;
      *pi = *pi + 1;
   }
   delete pi;
   delete[] pia;


                              2.3.  

           ,  
 ++.       
 int.          
  .
       ,  ,     
. ,        
,       .     ,
    :

  1.      .      
      ;
  2.        ;
  3.    , :    
      ;       ;
      ,      ,   ;
  4.   .     , - 
         :  -   .
  5. ,         , 
     :      . (    ,
           .)
  6.      .
  7.       .   
           .
  8.        
       .       
       ,      
                 ,
              
      ++.

                                                                    ++   46

   ,       
,        .  
    ++?       
 :

   class classname {
   public:
      //   
   private:
      //  ,  
   };

class, public  private     ++,  classname  , 
   .     IntArray: 
        .    
    ,      Array.
 ,     .      
 ,      .     
  ,      :

   //    IntArray
   IntArray myArray;
   //      IntArray
   IntArray *pArray = new IntArray;

        :  (,  
 class)  ,    .    
  .

   //   
   class IntArray;

              
public, private  protected. (        
protected.)     ,   
,  ,  ,    ,
   . ,  , 
- , -,  .    
IntArray:

                                                                ++   47

   class IntArray {
   public:
      //  : #2b
      bool operator== (const IntArray&) const;
      bool operator!= (const IntArray&) const;

      //  : #2a
      IntArray& operator= (const IntArray&);

      int size() const; // #1
      void sort(); // #4

      int min() const; // #3a
      int max() const; // #3b

      //  find   
      //   
      //  -1,    
   int find (int value) const; // #3c

   private:
      //    ,
      //   
      ...
   };

   ,      ,  
 ,       .   
     const,        ,
      .  ,   
     .
    - (, min())     
       .   , 
 (.),    ,    (->)    
. ,      ,   IntArray, 
 :

   //   min_val
   int min_val = myArray.min();
   //   myArray

         IntArray,
  :

   int min_val = pArray->min();

(,       ,       
    .     -,
 .      .)
          ,     
.       IntArray:

                                                                ++   48

   IntArray myrray0, myArray1;

           
 :

   //   -
   //  - myArray0.operator=(myArray1)
   myArray0 = myArray1;

   //   -
   //  - myArray0.operator==(myArray1)
   if (myArray0 == myArray1)
      cout << "!   !\n";

     public  private      .
  ,    public,     
,   ,    private,   
-  . ( -,   -
 ,           15.2.)
            ,  
 ,    .   
   .
            
,  .     , 
      .   ,    
 :

         ,   
     ,    ,  
    ,   :      (
         6.18);
        ,    
       ,   
    ,     ,    .

           IntArray? 
-       .      
 ,     .    
   .       -:

   class IntArray {
   public:
      // ...
      int size() const { return _size; }
   private:
      //  -
      int _size;
      int *ia;
   };

                                                               ++   49

       _size   ,    
    .     
 ,   - size(),    
_size.          
_size,  -   size()  .   
        .
    ,         , 
    .    (, 
     _size  public):

   IntArray array;
   int array_size = array.size();
   array_size = array._size;

   ,     ,     ,
   .   ,     
 ?
     , . ++    (inline) . 
      ,    
. (   ,    ,   
  ++.    ,       .)
    .       :

   for (int index=0; index<array.size(); ++index)
      // ...

  size()    _size    .  
   ,       
  ,     :

   for (int index=0; index<array._size; ++index)
   // ...

         (   ),  
 .     inline,  
  3.
           ,     .
          (  )
  ,      . 
   , ++   
       .

   3   inline      . 
      , 
 .       7.6.

                                                                   ++   50

       -,    
   .    ,  
     .
   - ,    ,    ,  . (
   ,    - -
.)   ,     .   : 
   ?
    ++  .         ,   
   /  .   
.    ,     
 ,     .      
  ,        
 .  .     
  min(). (    ,  
-.)

   //    min()
   //       
   #include <string>

   int min (const int *pia,int size);
   int min (int, int);
   int min (const char *str);
   char min (string);
   string min (string,string);

            
 .        
   . (  9    .)
,     IntArray.     
:

   class IntArray {
   public:
      explicit IntArray (int sz = DefaultArraySize);
      IntArray (int *array, int array_size);
      IntArray (const IntArray &rhs);
      // ...
   private:
      static const int DefaultArraySize = 12;
   }

      

   IntArray (int sz = DefaultArraySize);

   ,       
. (      explicit.)   
     int, 

                                                               ++   51

   IntArray array1(1024);

  1024    .     , :

   IntArray array2;

        
DefaultArraySize. (       static 
  DefaultArraySize:      13.5.  ,
         
    .)
           :

   IntArray::IntArray (int sz)
   {
      //   
      _size = sz;
      ia = new int[_size];

      //   
      for (int ix=0; ix<_size; ++ix)
         ia[ix] = 0;
   }

         .  
  ,       
.   ? -,  new  
    :      . (
 2.6  ,    .)  -, 
sz -      ,  
 .
          ?    
 ,        (::):

   IntArray::IntArray(int sz);

     ,     - (   ) 
 .    ,        
 IntArray,          
. (      8;  
      13.9.)
      IntArray   IntArray 
   .    :  
        .   
   IntArray    :

   int ia[10] = {0,1,2,3,4,5,6,7,8,9};
   IntArray iA3(ia,10);

                                                                ++   52

         
. (    ,      .)

   IntArray::IntArray (int *array, int sz)
   {
      //   
      _size = sz;
      ia = new int[_size];

      //   
      for (int ix=0; ix<_size; ++ix)
         ia[ix] = array[ix];
   }

       .   
  IntArray    IntArray.  
     :

   IntArray array;

   //     :
   IntArray ia1 = array;
   IntArray ia2 (array);

          IntArray, - 
 :

   IntArray::IntArray (const IntArray &rhs )
   {
      //   
      _size = rhs._size;
      ia = new int[_size];

      //   
      for (int ix=0; ix<_size; ++ix)
         ia[ix] = rhs.ia[ix];
   }

                , 
  &.      :
      .    
:     ,      , 
 ,   ; ,   rhs._size,   rhs->_size.
(    3.6.)
   ,       .      
   ,      . 
    ,   .   
  ,      
 init():

                                                                  ++   53

   class IntArray {
   public:
      explicit IntArray (int sz = DefaultArraySize);
      IntArray (int *array, int array_size);
      IntArray (const IntArray &rhs);
      // ...
   private:
      void init (int sz,int *array);
      // ...
   };

   // ,   
   void IntArray::init (int sz,int *array)
   {
      _size = sz;
       ia = new int[_size];
       for (int ix=0; ix<_size; ++ix)
         if ( !array )
            ia[ix] = 0;
         else
            ix[ix] = array[ix];
   }

   //  
   IntArray::IntArray (int sz) { init(sz,0); }

   IntArray::IntArray (int *array, int array_size)
   { init (array_size,array); }

   IntArray::IntArray (const IntArray &rhs)
   { init (rhs._size,rhs.ia); }

       -  ,  
   ,    .  
   ,       (~).  
    ,       
.       
,       .    IntArray 
-   ,     .
(       14.)   
  IntArray:

   public:
      // 
      explicit IntArray (int sz = DefaultArraySize);
      IntArray (int *array, int array_size);
      IntArray (const IntArray &rhs);

      // 
      ~IntArray() { delete[] ia; }
      // ...
   private:
      // ...
   };

                                                              ++   54

            IntArray. 
,     IntArray    ,   
  ,     :

   IntArray array;
   int last_pos = array.size()-1;
   int temp = array[0];
   array[0] = array[last_pos];
   array[last_pos] = temp;

          .  
 ,    :

   #include <cassert>

   int& IntArray::operator[] (int index)
   {
      assert (index >= 0 && index < _size);
      return ia[index];
   }

         , 
  , ,      
/.    ,  ,
  ,   .  ,  
          
 . ,        . ( 
      15,    3.15   
.)
    ,      ,  , -
         
,      .   IntArray   
  IntArray.h.  ,    
 IntArray,       
#include.
        - ,    ,
       ,    ++
.     . (,    
        ++ ) 
   IntArray.C.

                                     2.5

      ++     .
     (),  ; 
  ,     . 
      ,   . 
   ,  ,      ,

                                                            ++   55

   .      
  ( ):

   (a) 
   (b)  
   (c)   
   (d) 
   (e) 
   (f) 

                                     2.6

      ,   ,  
  .      ? ,  
     :       
 ,       
 .      ,   
 ,       
   - .

                                    2.7

          
  .    ,  
.     ?     
?          
 ?

                        2.4. - 

         .    ,
      ,    
,  ,   .   ,
   IntArray ,    ,  min(), max(),
find(),         
.   ,       
     .
          .      
 ,    
 ?        
?
          :    
    .    ,  
    ,      
.  ,  ,    .
       , 
,   .
   ,       ,     
,  ,  IntArray.  ,    
 ,        

                                                                ++   56

     . ,   ,
         :

   //      
   class IntArray { ... };

   //      
   class IntArrayRC { ... };

   //      
   class IntSortedArray { ... };

       :

        ,  
    .      .  
    ,     ,   
        ,     
    ;
     -       ,
         ,     
    :

     void process_array (IntArray&);
     void process_array (IntArrayRC&);
     void process_array (IntSortedArray&);

           .  
       ,       
      ,      ,  , ,
     .

    -    
 .       . 
     (, IntArrayRC  
IntArray),         -
,   -.    IntArrayRC  
   ,   IntArray, 
  ,        .
    ++ ,   ,    ,  -
   ,   .    
 ,    (..    -
  ). , ,     
 ,      ,   
,        .    
     .    
    .    
  swap(),       .
        IntArray:

                                                                   ++   57

   #include <IntArray.h>

   void swap (IntArray &ia, int i, int j)
   {
      int temp ia[i];
      ia[i] = ia[j];
      ia[j] = temp;
   }

   //      swap:
   IntArray ia;
   IntArrayRC iarc;
   IntSortedArray ias;

   //  - ia   IntArray
   swap (ia,0,10);

   //  - iarc   IntArray
   swap (iarc,0,10);

   //  - ias   IntArray
   swap (ias,0,10);

   //  - string    IntArray
   string str("  IntArray!");
   swap (str,0,10);


           -.  ,
   swap()     . , 
swap()   IntArrayRC:

   swap (iarc,0,10);

         IntArrayRC,  

   swap (ias,0,10);

   IntSortedArray.     
  ++.
        IntArray    . 
    ?    . , 
        .  , 
,     ,   
   virtual.      
   ,       
       .
           
       (,   
   ),      
.           
 (public)   (private).
     ,       
, .        ,
  ,      -,  

                                                                   ++   58

     .     
   ,     
  -   .     
,    ,   (protected).  ,
  ,   -,  
. (       .)
      IntArray:

   class IntArray {
   public:
      // 
      explicit IntArray (int sz = DefaultArraySize);
      IntArray (int *array, int array_size);
      IntArray (const IntArray &rhs);

      //  
      virtual ~IntArray() { delete[] ia; }

      //  :
      bool operator== (const IntArray&) const;
      bool operator!= (const IntArray&) const;

      //  :
      IntArray& operator= (const IntArray&);
      int size() const { return _size; };

      //    ...
      virtual int& operator[](int index)
         { return ia[index]; }
      virtual void sort();

      virtual int min() const;
      virtual int max() const;
      virtual int find (int value) const;
   protected:
      static const int DefaultArraySize = 12;
      void init (int sz; int *array);
      int _size;
      int *ia;
   };

    - -   ,   
   .       ,  
    .
    ,   ,    , 
.    IntArray    
.
      ,   -  
   .     .  
 ,         
  IntArrayRC.      size()
   , ,    .

                                                                 ++   59

            
 .       ,   
 .       ( )
         , 
  .  :

   void init (IntArray &ia)
   {
      for (int ix=0; ix<ia.size(); ++ix)
         ia[ix] = ix;
   }

      ia     IntArray, IntArrayRC 
 IntSortedArray. - size()      
 .          
  ,    ,    ia,  
 .
(  17        .  
    ,      .)
        IntArrayRC:

   #ifndef IntArrayRC_H
   #define IntArrayRC_H

   #include "IntArray.h"

   class IntArrayRC : public IntArray {
   public:
      IntArrayRC( int sz = DefaultArraySize );
      IntArrayRC( const int *array, int array_size );
      IntArrayRC( const IntArrayRC &rhs );
      virtual int& operator[]( int ) const;
   private:
      void check_range( int ix );
   };
   #endif

          IntArrayRC.h.    ,
       IntArray.h.
     IntArrayRC      ,  
  IntArray:  IntArrayRC     
  ;       
.
      -  IntArray     IntArrayRC
,      .      .
   

class IntArrayRC : public IntArray

                                                              ++   60

     ,   IntArrayRC    IntArray, 
,  .   public      , 
      ,    
        .  
IntArrayRC      IntArray, , , 
     swap().  ,  IntArrayRC 
    IntArray.
         :

   IntArrayRC::operator[]( int index )
   {
      check_range( index );
      return _ia[ index ];
   }

        check_range():

   #include <cassert>

   inline void IntArrayRC::check_range(int index)
   {
      assert (index>=0 && index < _size);
   }

   (    assert()  1.3.)
         ,      
  ?  ,   -   
-   ,     ,  
 assert(),    .
           ?
    ,   , 
   .      , 
     ,    
,      .
    ,      IntArrayRC   ,
 .   ,      
 ?         
  .
      ,      , 
     ++.  ,   
      . 
.       IntArrayRC  :

   int ia[] = {0,1,1,2,3,5,8,13};
   IntArrayRC iarc(ia,8);

                                                              ++   61

    ia  8    IntArray. 
    .    
  IntArrayRC:

   inline IntArrayRC::IntArrayRC( int sz )
      : IntArray( sz ) {}

   inline IntArrayRC::IntArrayRC( const int *iar, int sz )
      : IntArray( iar, sz ) {}

(        14  17.    ,
       IntArrayRC.)
    ,   ,    .
 ,    ,     .
   ,       
   .      
IntArrayRC,      .   ,    
      ,   ,
      ,  
:   ,  .  , 
       IntArrayRC,     
  .
         IntArrayRC     
IntArrayRC.h.      ,   
IntArrayRC.C  .
      ,   IntArray  IntArrayRC:

                                                                   ++   62


   #include <iostream>
   #include "IntArray.h"
   #include "IntArrayRC.h"

   void swap( IntArray &ia, int ix, int jx )
   {
      int tmp = ia[ ix ];
      ia[ ix ] = ia[ jx ];
      ia[ jx ] = tmp;
   }

   int main()
   {
      int array[ 4 ] = { 0, 1, 2, 3 };
      IntArray ia1( array, 4 );
      IntArrayRC ia2( array, 4 );

      // :   size-1
      //      IntArray
      cout << "swap() with IntArray ia1" << endl;
      swap( ia1, 1, ia1.size() );

      // :  IntArrayRC "" 
      cout << "swap() with IntArrayRC ia2" << endl;
      swap( ia2, 1, ia2.size() );
      return 0;
   }

        :

   swap() with IntArray ia1
   swap() with IntArrayRC ia2
   Assertion failed: ix >= 0 && ix < _size,
      file IntArrayRC.h, line 19

                                    2.8

           
. ,  IntArrayRC    IntArray,  
   ,     
..     ?

   (a) -   
   (b) -   
   (c)    -
   (d)     
   (e)    
   (f)     
   (g)    
   (h)    
   (i)    

                                                              ++   63

                                     2.9

   ,         
  ,  ,     :

   (a) rotate();
   (b) print();
   (c) size();
   (d) DateBorrowed(); //   
   (e) rewind();
   (f) borrower(); // 
   (g) is_late(); //  
   (h) is_on_loan(); //  

                                   2.10

      ,       
 .  ,        
        . 
  ,       
         
   .       ?

                                   2.11

           
   .  ,       
,          
   ,  -  
 ,        ,
   .   ,  
   ,      
 .   ?

                                   2.12

           , ,
,      ,
, .         
.       , 
.   .   
,   .

   (a) 
   (b) 
   (c) 
   (d) _
   (e) __

                                                                ++   64

   (f) _

                            2.5.  

     IntArray       .
          . 
,       double  
    ,     .
        ++   .   
     . -
     ,  
 .      Array,  
 IntArray   int   -.  
 -,      int, double 
string.        ,   
           .
      Array:

   template <class elemType>
   class Array {
   public:
       explicit Array( int sz = DefaultArraySize );
       Array( const elemType *ar, int sz );
      Array( const Array &iA );
      virtual ~Array() { delete[] _ia; }
      Array& operator=( const Array & );
      int size() const { return _size; }
      virtual elemType& operator[]( int ix )
      { return _ia[ix]; }
      virtual void sort( int,int );
      virtual int find( const elemType& );
      virtual elemType min();
      virtual elemType max();
   protected:
      void init( const elemType*, int );
      void swap( int, int );
      static const int DefaultArraySize = 12;
      int _size;
      elemType *_ia;
   };

     template   ,   ,  
    (<>).       
elemType;   class    ,   
  .
     - Array  elemType   
   ,    :

                                                                    ++   65

   #include <iostream>
   #include "Array.h"

   int main()
   {
      const int array_size = 4;

      // elemType   int
      Array<int> ia(array_size);

      // elemType   double
      Array<double> da(array_size);

      // elemType   char
      Array<char> ca(array_size);

      int ix;
      for ( ix = 0; ix < array_size; ++ix ) {
         ia[ix] = ix;
         da[ix] = ix * 1.75;
         ca[ix] = ix + 'a';
      }
      for ( ix = 0; ix < array_size; ++ix )
         cout << "[ " << ix << " ] ia: " << ia[ix]
                << "\tca: " << ca[ix]
                << "\tda: " << da[ix] << endl;
      return 0;
   }

        Array:

   Array<int> ia(array_size);
   Array<double> da(array_size);
   Array<char> ca(array_size);

     ,   ?    Array,
  elemType   ,      .
,        :


// Array<int> ia(array_size);
int _size;
int *_ia;

   ,        IntArray.
       :

   // Array<double> da(array_size);
   int _size;
   double *_ia;

                                                          ++   66

   // Array<char> ca(array_size);
   int _size;
   char *_ia;

      -?    - elemType 
  ,      ,  
  -  . (     16.8.)
          :

   [ 0 ] ia: 0 ca: a da: 0
   [ 1 ] ia: 1 ca: b da: 1.75
   [ 2 ] ia: 2 ca: c da: 3.5
   [ 3 ] ia: 3 ca: d da: 5.25

          .   
   ArrayRC:

   #include <cassert>
   #include "Array.h"

   template <class elemType>
   class ArrayRC : public Array<elemType> {
   public:
      ArrayRC( int sz = DefaultArraySize )
         : Array<elemType>( sz ) {}
      ArrayRC( const ArrayRC& r )
         : Array<elemType>( r ) {}
      ArrayRC( const elemType *ar, int sz )
        : Array<elemType>( ar, sz ) {}
      elemType& ArrayRC<elemType>::operator[]( int ix )
      {
         assert( ix >= 0 && ix < Array<elemType>::_size );
         return _ia[ ix ];
      }
   private:
      // ...
   };

       - elemType   
,     . 

     ArrayRC<int> ia_rc(10);

    ,   IntArrayRC   . 
    .  ,  

    //  swap()    
    swap( ia1, 1, ia1.size() );

 ,     swap()   .

                                                               ++   67

   #include "Array.h"

   template <class elemType>
   inline void
   swap( Array<elemType> &array, int i, int j )
   {
      elemType tmp = array[ i ];
      array[ i ] = array[ j ];
      array[ j ] = tmp;
   }

      swap()   ,   
 .    ,   Array  ArrayRC:

   #include <iostream>
   #include "Array.h"
   #include "ArrayRC.h"

   template <class elemType>
   inline void
   swap( Array<elemType> &array, int i, int j )
   {
      elemType tmp = array[ i ];
      array[ i ] = array[ j ];
      array[ j ] = tmp;
   }

   int main()
   {
      Array<int> ia1;
      ArrayRC<int> ia2;

      cout << "swap() with Array<int> ia1" << endl;
      int size = ia1.size();
      swap( ia1, 1, size );

      cout << "swap() with ArrayRC<int> ia2" << endl;
      size = ia2.size();
      swap( ia2, 1, size );

      return 0;
   }

                                          2.13

        :

   template<class elemType> class Array;
   enum Status { ... };
   typedef string *Pstring;

          ?

                                                                   ++   68

   (a) Array< int*& > pri(1024);
   (b) Array< Array<int> > aai(1024);
   (c) Array< complex< double > > acd(1024);
   (d) Array< Status > as(1024);
   (e) Array< Pstring > aps(1024);

                                          2.14

     ,     :

   class example1 {
   public:
      example1 (double min, double max);
      example1 (const double *array, int size);
      double& operator[] (int index);
      bool operator== (const example1&) const;
      bool insert (const double*, int);
      bool insert (double);
      double min (double) const { return _min; };
      double max (double) const { return _max; };
      void min (double);
      void max (double);
      int count (double value) const;
   private:
      int size;
      double *parray;
      double _min;
      double _max;
   }

                                          2.15

      :

   template <class elemType> class Example2 {

   public:
      explicit Example2 (elemType val=0) : _val(val) {};
      bool min(elemType value) { return _val < value; }
      void value(elemType new_val) { _val = new_val; }
      void print (ostream &os) { os << _val; }
   private:
      elemType _val;
   }

   template <class elemType>

                                                            ++   69

   ostream& operator<<(ostream &os,const Example2<elemType> &ex)
      { ex.print(os); return os; }

    ?

   (a) Example2<Array<int>*> ex1;
   (b) ex1.min (&ex1);
   (c) Example2<int> sa(1024),sb;
   (d) sa = sb;
   (e) Example2<string> exs("Walden");
   (f) cout << "exs: " << exs << endl;

                                     2.16

           
,      elemType. ,  
    0:

   explicit Example2 (elemType val=0) : _val(val) {};

           (,  string),
  

   Example2<string> exs("Walden");

 , 

   Example2<string> exs2;

   4.      min(), 
      . ++    
 ,   .   ,     
?  ,      
     Example2.  ,  .

                                2.17

        ,    Example2
 ,      ,   
.     ,    
     min(),   
  .   ,    ?  
    ,    ?  
.

4       :
Example2( elemType nval = elemType() ) " _val( nval ) {}

                                                               ++   70

                          2.6.  

      ,    
:        
,       - .
   ,  ,    
 ,      
  ,        
       .
++      .   
   ,      ,
        . 
     ,   ,   
   .
         :
    ,    .   ,  
  - ,     . 
  ++    throw.   
       :

   if ( !infile ) {
      string errMsg("  : ");
      errMsg += fileName;
      throw errMsg;
   }

    ,    .  
      
  .       
     .   
  ,     ,   , 
 ,   .      ++
   catch.        
 :

   catch (string exceptionMsg) {
      log_message (exceptionMsg);
      return false;
   }

    catch-   ,   
,       
 try.  try-    catch-,
       .  :

   int* stats (const int *ia, int size)
   {
      int *pstats = new int [4];

                                                               ++   71

      try {
         pstats[0] = sum_it (ia,size);
         pstats[1] = min_val (ia,size);
         pstats[2] = max_val (ia,size);
      }
      catch (string exceptionMsg) {
          //  
      }
      catch (const statsException &statsExcp) {
         //  
      }
          
      pstats [3] = pstats[0] / size;
      do_something (pstats);
        
      return pstats;
   }

         stats()     try-, 
  .        .

   1) int *pstats = new int [4];

     new   .   ++
   bad_alloc    
  .      
 bad_alloc,      
.

   2) do_something (pstats);

        do_something().    ,
 ,    ,  ,   ,
  ,   ,    . 
   do_something      
 ,   stats()   .  
  ,    .
    , ,  

   pstats [3] = pstats[0] / size;

     ,       
.
      ,   try-.     
     sum_it(), min_val()  max_val() 
,     ,   try- 
   .  ,  , 
     try-   .  , 
   sum_it()  :

   throw string (": adump27832");

     sum_it() , ,   try- 
  ,    ,  pstats[0]  
.       
 catch-.     catch    string:

                                                              ++   72

   catch (string exceptionMsg) {
      //  
   }

        ,   
catch-,    try-.    

   pstats [3] = pstats[0] / size;

(,     ,       . 
     catch-,  
,   stats().)
    :

   catch (string exceptionMsg) {
     //  
     cerr << "stats(): : "
     << exceptionMsg
     << endl;
     delete [] pstats;

     return 0;
   }

         ,  stats().  ,
       stats()
      .
    stats()      : string 
statsException.     ,  
   ,        ,   
   ,     main().  
 ,   .
      ,     
.   :

   catch (...) {
      //   ,
      //    , 
      //     throw
   }

(       11  19.)

                                   2.18

            :

   int *alloc_and_init (string file_name)
   {
      ifstream infile (file_name)

                                                                   ++   73

      int elem_cnt;
      infile >> elem_cnt;
      int *pi = allocate_array(elem_cnt);

      int elem;
      int index=0;
      while (cin >> elem)
         pi[index++] = elem;

      sort_array(pi,elem_cnt);
      register_data(pi);

       return pi;
   }

                                        2.19

        allocate_array(), sort_array() 
register_data()     noMem, int  string
.   alloc_and_init(),  
 try  catch    .     
cerr   .

                                       2.20

     alloc_and_init() ,    
       (  
,     allocate_array(), sort_array()
 register_data()  -     alloc_and_init()).
     string  ,  , 
 .

                        2.7.   

   ,          Array,
   .      
; , - -, ,      Intel
   . -      ,
       , 
    .       
 ,   , ,

   class Cplusplus_Primer_Third_Edition_Array { ... };

   ,      ,    
    . , ,   
 !
    ++       ,
  .    
   ,       
 .   , ,    Array:

                                                               ++   74

   namespace Cplusplus_Primer_3E {
      template <class elemType> class Array { ... };

   }

     namespace   ,   
      Cplusplus_Primer_3E. ,    
   ,     :

   namespace IBM_Canada_Laboratory {
      template <class elemType> class Array { ... };
      class Matrix { ... };
   }

   namespace Disney_Feature_Animation {
      class Point { ... };
      template <class elemType> class Array { ... };
   }

        ,    
 ;      .   
     ,   
 ,       
 ,      (::).
        :

   Cplusplus_Primer_3E::Array<string> text;
   IBM_Canada_Laboratory::Matrix mat;
   Disney_Feature_Animation::Point origin(5000,5000);

          .
      . :

   // 
   namespace LIB = IBM_Canada_Laboratory;
   namespace DFA = Disney_Feature_Animation;

   int main()
   {
      LIB::Array<int> ia(1024);
   }

       ,     .
 ,        ,
        .   
    ,      
:

   namespace LIB = Cplusplus_Primer_3E;
   int main()
   {
      LIB::Array<int> ia(1024);
   }

                                                            ++   75

   ,    ,    
  ,     . ,  
Array  Disney_Feature_Animation       
.     :

   namespace LIB = Disney_Feature_Animation;

   int main()
   {
      LIB::Array<int> ia(1024);
   }

         , 
    ,     . 
   using:

   #include "IBM_Canada_Laboratory.h"

   using namespace IBM_Canada_Laboratory;

   int main()
   {
      // IBM_Canada_Laboratory::Matrix
      Matrix mat(4,4);

      // IBM_Canada_Laboratory::Array
      Array<int> ia(1024);
      // ...
   }

     IBM_Canada_Laboratory    . 
    ,      (
 using):

   #include "IBM_Canada_Laboratory.h"

   using namespace IBM_Canada_Laboratory::Matrix;
   //    Matrix

   int main()
   {
      // IBM_Canada_Laboratory::Matrix
      Matrix mat(4,4);

      // : IBM_Canada_Laboratory::Array 
      Array<int> ia(1024);
      // ...
   }

      ,     ++  
  std.      ,
      :

   #include <string>

   // : string 
   string current_chapter = " ++";

                                                                ++   76

      using:

   #include <string>
   using namespace std;

   // Ok:  string
   string current_chapter = " ++";

   , ,        
  ,       
 .      :

   #include <string>

   // :  
   std::string current_chapter = " ++";
 
      using:

   #include <string>
   using namespace std::string;

   // Ok: string 
   string current_chapter = " ++";

       .
            . 
    ,   ,    
 ,      
  ++. (  using-   
  ++    8.6.)
          : String, Stack, List 
 Stack.         
Cplusplus_Primer_3E. (      
  8.)

                                    2.21

     

   namespace Exercize {

      template <class elemType>
         class Array { ... };

      template <class EType>
         void print (Array< EType > );

      class String { ... }
      template <class ListType>
      class List { ... };
   }

  :

                                                                   ++   77
   int main() {
      const int size = 1024;
      Array<String> as (size);
      List<int> il (size);

      // ...

      Array<String> *pas = new Array<String>(as);
      List<int> *pil = new List<int>(il);

      print (*pas);
   }

     ,      
  Exercise.   , 

   (a)  
   (b)   using
   (c)  
   (d)  using

                      2.8.     

          , ,  
 ,     .  
 C++         
  ,    .   
     C++.     ,
 .
   ,      . ,   

   vector<int> ivec(10);
   vector<string> svec(10);

           Array  
  vector.     ,    
   ,    
,       ,   
   .      
   .    
  -,   , ,  sort(), min(),
max(), find()  ,  vector   : 
     , size()  empty().   ,
 ,     .
      vector     
.

   #include <vector>

                                                                ++   78

   //      vector
   vector<int> vec0; //  

   const int size = 8;
   const int value = 1024;

   //   8
   //    0
   vector<int> vec1(size);

   //   8
   //     1024
   vector<int> vec2(size,value);

   //   4
   //     ia
   int ia[4] = { 0, 1, 1, 2 };
   vector<int> vec3(ia,ia+4);

   // vec4 -  vec2
   vector<int> vec4(vec2);

    ,    Array,  vector     .
     :

   #include <vector>
   extern int getSize();

   void mumble()
   {
      int size = getSize();
      vector<int> vec(size);
      for (int ix=0; ix<size; ++ix)
         vec[ix] = ix;
      // ...
   }

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

   #include <vector>
   extern int getSize();

   void mumble()
   {
      int size = getSize();
      vector<int> vec(size);

                                                                    ++   79

      vector<int>::iterator iter = vec.begin();
      for (int ix=0; iter!=vec.end(); ++iter, ++ix)
         *iter = ix;
      // ...
   }

     iter

   vector<int>::iterator iter = vec.begin();

      vec. iterator  
 typedef    vector,    int. 


   ++iter

     .    ,
   :

   *iter

      ++    ,  
 vector,     - ,   
 .    :

 : find(), find_if(), search(), binary_search(), count(),
                  count_if();

   : sort(), partial_sort(), merge(),
                  partition(), rotate(), reverse(), random_shuffle();

 : unique(), remove();

 : accumulate(), partial_sum(), inner_product(),
                     adjacent_difference();

    : generate(), fill(),
                     transform(), copy(), for_each();

 : equal(), min(), max().

           , 
  ,    . , 
     ivec,   :

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

      sort()     ,  :

   sort ( ivec.begin(), ivec.begin() + ivec.size()/2 );

             
. , ,   :

                                                             ++   80

   int ia[7] = { 10, 7, 9, 5, 3, 7, 1 };

         sort():

   sort ( ia, ia+7 );

        :

   sort ( ia, ia+4 );

           

   #include <algorithm>

      ,    
    vector:

                                                          ++   81

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

   int ia[ 10 ] = {
      51, 23, 7, 88, 41, 98, 12, 103, 37, 6
   };

   int main()
   {
      vector< int > vec( ia, ia+10 );
      vector<int>::iterator it = vec.begin(), end_it = vec.end();

      cout << " : ";
      for ( ; it != end_it; ++ it ) 
         cout << *it << ' '; cout << "\n";

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

      cout << " : ";
      it = vec.begin(); end_it = vec.end();
      for ( ; it != end_it; ++ it ) cout << *it << ' ';
         cout << "\n\n";

      int search_value;
      cout << "   : ";
      cin >> search_value;

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

      if ( found != vec.end() )
         cout << " !\n\n";
      else cout << " !\n\n";

      //  
      reverse( vec.begin(), vec.end() );

      cout << " : ";
      it = vec.begin(); end_it = vec.end();

      for ( ; it != end_it; ++ it ) cout << *it << ' ';
         cout << endl;

   }

     ++    . 
   ,       
,     .    
    (map). , 
       ,  
  ,      :

   #include <map>
   #include <string>
   #include "TelephoneNumber.h"

                                                             ++   82

   map<string, telephoneNum> telephone_directory;

( ,        
 6.      ,   . 
 12   ,      
.)
             
++,  -      
  .      
    .

                                    2.22

          :

   string pals[] = {
      "pooh", "tiger", "piglet", "eeyore", "kanga" };

   (a) vector<string> svec1(pals,pals+5);
   (b) vector<int> ivec1(10);
   (c) vector<int> ivec2(10,10);
   (d) vector<string> svec2(svec1);
   (e) vector<double> dvec;

                                     2.23

       min(),    .
     .   for 
   


   template <class elemType>



   elemType min (const vector<elemType> &vec);
