vector支持很多种数据类型,故要定义成模板类
int theSize;
int theCapacity;
T* array;
#define WALK_LENGTH 64;
myVector():theSize(0),theCapacity(0),array(NULL){}
myVector(const T& target , int num):theSize(0),theCapacity(0),array(NULL)
{
while( num-- )
push_back(target);
}
myVector(const myVector<T>& other):theSize(0),theCapacity(0),array(NULL)
{
// 已重载本类=操作符,有开辟新空间,仍属于深拷贝
*this = other; //重载=
}
~myVector()
{
clear();
}
int size() const
{
return theSize;
}
int capacity() const
{
return theCapacity;
}
bool empty()
{
return theSize==0;
}
void clear()
{
if( array )
delete array;
array = NULL;
theSize = 0;
theCapacity = 0;
}
void printArray()
{
for( int i=0; i<theSize; i++ )
cout<<array[i]<<" ";
cout<<",size="<<theSize<<" ,capacity="<<theCapacity<<endl;
}
void push_back(const T& target)
{
insert_before(theSize, target);
}
void push_front(const T& target)
{
insert_before(0, target);
}
void insert_before(const int& pos, const T& target)
{
if(theSize == theCapacity)
{
/* array没有delete之前,原来的空间仍然存在,
/* 当array申请了新空间,只是失去了旧空间的指向,
/* 用oldarray指向旧空间,等新空间拷贝完,再delete旧空间
*/
T* oldarray = array;
theCapacity += WALK_LENGTH;
array = new T[theCapacity];
for( int i=0; i<theSize; i++ )
array[i] = oldarray[i];
delete oldarray;
}
for( int i=theSize; i>pos; i-- )
array[i] = array[i-1];
array[pos] = target;
theSize++;
}
void erase(const int& pos)
{
if( pos < theSize )
{
for( int i=pos; i<theSize; i++ )
array[i] = array[i+1];
theSize--;
}
}
myVector<T>& operator = (const myVector<T>& other)
{
//参数other为const 所以other调用的函数都应定义为const
if( this == &other )
return *this;
clear();
theSize = other.size();
theCapacity = other.capacity();
array = new T[theCapacity];
for( int i=0; i<theSize; i++ )
array[i] = other[i]; //重载[]
return *this;
}
T& operator [] ( const int& pos ) const
{
assert(pos<theSize);
return array[pos];
}
#include <iostream>
#include <assert.h>
using namespace std;
template<typename T>
class myVector
{
private:
int theSize;
int theCapacity;
T* array;
#define WALK_LENGTH 64;
public:
myVector():theSize(0),theCapacity(0),array(NULL){}
myVector(const T& target , int num):theSize(0),theCapacity(0),array(NULL)
{
while( num-- )
push_back(target);
}
myVector(const myVector<T>& other):theSize(0),theCapacity(0),array(NULL)
{
// 已重载本类=操作符,有开辟新空间,仍属于深拷贝
*this = other; //重载=
}
~myVector()
{
clear();
}
myVector<T>& operator = (const myVector<T>& other)
{
//参数other为const 所以other调用的函数都应定义为const
if( this == &other )
return *this;
clear();
theSize = other.size();
theCapacity = other.capacity();
array = new T[theCapacity];
for( int i=0; i<theSize; i++ )
array[i] = other[i]; //重载[]
return *this;
}
T& operator [] ( const int& pos ) const
{
assert(pos<theSize);
return array[pos];
}
void clear()
{
if( array )
delete array;
array = NULL;
theSize = 0;
theCapacity = 0;
}
int size() const
{
return theSize;
}
int capacity() const
{
return theCapacity;
}
bool empty()
{
return theSize==0;
}
void push_back(const T& target)
{
insert_before(theSize, target);
}
void push_front(const T& target)
{
insert_before(0, target);
}
void insert_before(const int& pos, const T& target)
{
if(theSize == theCapacity)
{
/* array没有delete之前,原来的空间仍然存在,
/* 当array申请了新空间,只是失去了旧空间的指向,
/* 用oldarray指向旧空间,等新空间拷贝完,再delete旧空间
*/
T* oldarray = array;
theCapacity += WALK_LENGTH;
array = new T[theCapacity];
for( int i=0; i<theSize; i++ )
array[i] = oldarray[i];
delete oldarray;
}
for( int i=theSize; i>pos; i-- )
array[i] = array[i-1];
array[pos] = target;
theSize++;
}
void erase(const int& pos)
{
if( pos < theSize )
{
for( int i=pos; i<theSize; i++ )
array[i] = array[i+1];
theSize--;
}
}
void printArray()
{
for( int i=0; i<theSize; i++ )
cout<<array[i]<<" ";
cout<<",size="<<theSize<<" ,capacity="<<theCapacity<<endl;
}
};
int main()
{
myVector<int> vec1; //无参数构造函数
cout<<"相同值赋值的构造函数"<<endl;
myVector<int> vec2(2,4);
vec2.printArray();
cout<<"拷贝构造函数"<<endl;
myVector<int> vec3(vec2);
vec3.printArray();
cout<<"在头插入 1"<<endl;
vec2.push_front(1);
vec2.printArray();
cout<<"删除位置11"<<endl;
vec2.erase(11);
vec2.printArray();
cout<<"删除位置0"<<endl;
vec2.erase(0);
vec2.printArray();
cout<<"在尾插入3"<<endl;
vec2.push_back(3);
vec2.printArray();
cout<<"在位置2之前插入4"<<endl;
vec2.insert_before(2,4);
vec2.printArray();
cout<<"操作符=重载"<<endl;
vec1 = vec2;
vec1.printArray();
return 0;
}
执行结果: