复习(数据结构):动态数组:c++_stl写法

来源:互联网 发布:minidao sql 编辑:程序博客网 时间:2024/05/22 17:36
  • 定义异常
//"dsexceptions.h"#ifndef DS_EXCEPTIONS_H#define DS_EXCEPTIONS_Hclass 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:    /*    http://www.cnblogs.com/ymy124/p/3632634.html    explicit修饰构造函数,防止发生隐形类型转换    */    //构造函数    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;}        //:const类成员变量是在对象的生存期内是常量.以上enum常量的优点是不占用对象的存储空间,且在编译时全部求值.        //上述数组值的大小头文件中求值只有依赖于枚举        //        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;}
0 0
原创粉丝点击