apvector class

 

 

 

templat­e <class itemType>

class apvector

{

      // constructors/destructor

      apvector( );                                                                       // default constructor (size==0)

      explicit apvector( int size );                                              // initial size of vector is size

      apvector( int size, const itemType & fillValue );              // all entries == fillValue

      apvector( const apvector & vec );                                    // copy constructor

      ~apvector( );                                                                     // destructor

 

      // assignment

      const apvector & operator = ( const apvector & vec );

 

      // accessors

      int  length( ) const;                                                            // capacity of vector

 

      // indexing

      itemType &       operator [ ] ( int index );                         // indexing with range checking

      const itemType & operator [ ] ( int index ) const;            // indexing with range checking

 

      // modifiers

      void resize( int newSize );                                                  // change size dynamically;

};

 

 

Note:  This is the latest version of the apvector class downloaded from College Board (4/17/98), www.CollegeBoard.org.


 

Constuctors                                                                            Sample syntax                          Comments

 

 

apvector( );

 

 

apvector<int> list;

 

Default constructor, the array is declared but has no memory allocated yet

 

explicit apvector( int size );

 

 

apvector<int> list(10);

 

Constructs an array with size entries

 

apvector( int size, const itemType & fillValue );

 

 

apvector<int> list(10,0);

 

Constructs an array and fills all entries with fillValue

 

apvector( const apvector & vec );

 

 

apvector<int> A(B);

 

Constructs A as a copy of B, assumes that array B already exists

 

 

Member functions

 

 

Sample syntax

 

 

Comments

 

 

const apvector & operator = ( const apvector & vec );

 

A = B;

 

Array A is now a copy of array B, old values of A are lost, the capacity of A now equals the capacity of B

 

int  length( ) const;

 

 

int  n = A.length();

 

Returns the capacity of the array

 

itemType &       operator [ ] ( int index );      

const itemType & operator [ ] ( int index ) const;

 

 

cout << A[k];

A[k] = 5;

 

Array access

 

void resize( int newSize );            

 

A.resize(20);

 

Resizes the array to newSize.  The algorithm uses linear copying of all elements in the array.