#include /* For now, the easiest way to use the token 'size_type' as a type. This however is NOT a new type. */ #define size_type unsigned int class vector{ private: //is the information container char* InnerArray; //The size of the allocated memory size_type m_size; //Ctr on the elements that are in the vector size_type m_filled_in; //Max size of vector static const size_type m_max_size = 1000000; //size of increment when the vector is too //small for insertion static const size_type m_inc_size = 10; void destroy(){ if(size()) delete InnerArray; InnerArray = NULL; }; public: ///Constructor, note the special notation for initialisation vector(size_type initialSize=0):m_size(initialSize),m_filled_in(0){ if(size() > max_size()) throw "Some Exception"; if(size())InnerArray = new char[size()]; }; bool empty() const{ //Note the const. In this case //it indicates that the method will not //modify the internal state of the object return size()==0; }; size_type size() const{ //Returns the size of the vector. return m_size; }; size_type max_size() const{ //Returns the largest possible size of the vector. return m_max_size; }; void push_back(const char& Elt){ //Back Insertion Sequence //Inserts a new element at the end. if(max_size() == m_filled_in){ throw "some exception that says the vector is at its max"; } if(m_filled_in < size( )){ InnerArray[m_filled_in] = Elt; m_filled_in++; return; } size_type NewSize = (size()+ m_inc_size < max_size())?size()+ m_inc_size:max_size(); resize(NewSize); return push_back(Elt); }; void resize(int n, char Default=(char)0){ //First do some verifications if((n > max_size()) || (n < 0)) throw "some exception that says that the argument is bad"; char* NewArray = new char[n]; int i=0; for(i; i size()) m_filled_in = size( ); }; void dump(){ printf("\nm_filled_in=%d, size( )=%d\n", m_filled_in, size( )); for(int i=0; i