#ifndef DS_EXCEPTIONS_H
#define DS_EXCEPTIONS_H
class UnderflowException { };
class IllegalArgumentException { };
class ArrayIndexOutOfBoundsException { };
class IteratorOutOfBoundsException { };
class IteratorMismatchException { };
class IteratorUninitializedException { };
#endif
#ifndef _VECTOR_H
#define _VECTOR_H
#include "dsexceptions.h"
template <typename Object>
class vector {
public :
explicit vector (int intSize=0 )
:theSize(initSize),theCapacity(intSize+SPACE_CAPACITY)
{objects=new Object[theCapacity];}
vector (const vector &rhs):objects(NULL)
{operator =(rhs);}
~vector ();
const vector & operator = (const vector & rhs){
if (this !=&rhs){
delete [] objects;
theSize = rhs.size();
theCapacity = rhs.theCapacity;
objects=new Object[capacity()];
for (int k=0 ;k<size();k++)
objects[k]=rhs.objects[k];
}
return *this ;
}
void resize( int newSize){
if (newSize>theCapacity)
reserve(newSize*2 +1 );
theSize=newSize;
}
void reserve(int newCapactity){
if (newCapactity<thesize)
retrun;
objects *oldArray = objects;
objects = new Object[newCapactity];
for (int k=0 ;k<theSize;k++)
objects[k]=oldArray[k];
theCapacity = newCapactity;
delete [] oldArray;
Object& operator [] (int index){
return objects[index];
}
const Object& operator [] (int index) const {
return objects[index];
}
bool empty() const {
return size()==0 ;
}
int size() const {
return theSize;
}
int capacity() const
{
return theCapacity;
}
void push_back(const Object& x){
if (theSize==theCapacity)
reserve(2 *theCapacity+1 );
objects[theSize++]=x;
}
void pop_back(){
theSize--;
}
const Object& back() const {
return objects[theSize -1 ];
}
typedef Object* iterator;
typedef const Object* const_iterator;
iterator begin(){
return &objects[0 ];
}
const_iterator begin() const
{return &objects[0 ];}
iterator end()
{return &objects[size()];}
enum {SPACE_CAPACITY=16 ;}
private :
int theSize;
int theCapacity;
Object* objects;
}
}
#endif
#ifndef VECTOR_CPP_
#define VECTOR_CPP_
#include "vector.h"
#include "StartConv.h"
template <class Object>
const vector <Object> & vector <Object> ::operator =( const vector <Object> & rhs )
{
if ( this != &rhs )
{
delete [ ] objects;
theSize = rhs.size( );
theCapacity = rhs.theCapacity;
objects = new Object[ capacity( ) ];
for ( int k = 0 ; k < size( ); k++ )
objects[ k ] = rhs.objects[ k ];
}
return *this ;
}
template <class Object>
void vector <Object> ::resize( int newSize )
{
if ( newSize > theCapacity )
reserve( newSize * 2 );
theSize = newSize;
}
template <class Object>
void vector <Object> ::reserve( int newCapacity )
{
Object *oldArray = objects;
int numToCopy = newCapacity < theSize ? newCapacity : theSize;
newCapacity += SPARE_CAPACITY;
objects = new Object[ newCapacity ];
for ( int k = 0 ; k < numToCopy; k++ )
objects[ k ] = oldArray[ k ];
theSize = numToCopy;
theCapacity = newCapacity;
delete [ ] oldArray;
}
template <class Object>
void vector <Object> ::push_back( const Object & x )
{
if ( theSize == theCapacity )
reserve( 2 * theCapacity + 1 );
objects[ theSize++ ] = x;
}
template <class Object>
void vector <Object> ::pop_back( )
{
if ( empty( ) )
throw UnderflowException( "Cannot call pop_back on empty vector" );
theSize--;
}
template <class Object>
const Object & vector <Object> ::back( ) const
{
if ( empty( ) )
throw UnderflowException( "Cannot call back on empty vector" );
return objects[ theSize - 1 ];
}
#include "EndConv.h"
#endif
* 测试程序
#include "Vector.h"
#include <iostream>
using namespace std ;
int main( )
{
Vector<int > v;
for ( int i = 0 ; i < 10 ; i++ )
v.push_back( i );
for ( int i = 0 ; i < 10 ; i++ )
cout << v[ i ] << endl;
return 0 ;
}