您的位置:首页 > 编程语言 > C语言/C++

c++ stl库 vector简单的实现

2012-03-31 18:39 459 查看
//stl库中vector是一个自动管理的动态数组;

//其实只要明白vector的类型是一个数组,怎么去实现它,其实就好办了;

//我选择了一种简单的方式去实现它;//定义一个步长(WALK_LENGTH);

//在数组空间不够的时候,重新申请 allocCount+WALK_LENGTH 长度的内存;

//这样避免了,每次vector元素增加的时候,去重新申请空间;

//当然,不好的地方也很明显。就是会浪费一定的空间。但是时间效率上会提高很多;

//因为vector,可以支持下标访问,而且效率很快,所以没必要去构造一个iterator;

//通过分析代码,可以发现vector的优缺点

//1、随即访问元素效率很高;

//2、push_back的效率也会很高;

//3、push_front的效率则非常的低,不建议使用;

//4、insert,需要把插入位置以后的元素,全部后移,效率比较低,不建议使用;

//5、erase,需要把删除位置后面的元素,全部前移,效率比较低,不建议使用;

//6、当内存不够的时候,需要重新申请内存,在把以前的元素拷贝过来,这个时候,效率也是比较低的;

//有不足的地方,还希望大家指正

#ifndef _MY_VECTOR_H_
#define _MY_VECTOR_H_

#include <assert.h>

template<typename T>
class myVector
{
private:
//walk length
//myVector each time increase space length
#define WALK_LENGTH 64;

public:
//default constructor
myVector():array(0),count(0),allocCount(0)
{

}

//
myVector(const T& t,unsigned int n):array(0),count(0),allocCount(0)
{
while(n--)
{
push_back(t);
}
}

//copy constructor
myVector(const myVector<T>& other):array(0),count(0),allocCount(0)
{
*this = other;
}

//= operator
myVector<T>& operator =(myVector<T>& other)
{
if(this == &other)
return *this;

clear();

count = other.size();
allocCount = other.allocSize();
array = new T[allocCount];
for(unsigned int i = 0 ;i<count;++i)
{
array[i] = other[i];
}

return *this;
}

//destructor
~myVector()
{
clear();
}

//the pos must be less than myVector.size();
T& operator[](unsigned int pos)
{
assert(pos<count);
return array[pos];
}

//element count
unsigned int size()
{
return count;
}

//alloc count
unsigned int allocSize()
{
return allocCount;
}

//is  empty
bool empty()
{
return count == 0;
}

//clear myVector
void clear()
{
deallocator(array);
array = 0;
count = 0;
allocCount = 0;
}

//adds an element in the back of myVector
void push_back(const T& t)
{
insert_after(count-1,t);
}

//adds an element int the front of myVector
void push_front(const T& t)
{
insert_before(0,t);
}

//inserts an element after the pos
//the pos must be in [0,count);
void insert_after(int pos,const T& t)
{
insert_before(pos+1,t);
}

//inserts an element before the pos
//the pos must be less than the myVector.size()
void insert_before(int pos,const T& t)
{
if(count==allocCount)
{
T* oldArray = array;

allocCount += WALK_LENGTH;
array = allocator(allocCount);
//memcpy(array,oldArray,count*sizeof(T)):
for(unsigned int i = 0 ;i<count;++i)
{
array[i] = oldArray[i];
}
deallocator(oldArray);
}

for(int i = (int)count++;i>pos;--i)
{
array[i] = array[i-1];
}
array[pos] = t;
}

//erases an element in the pos;
//pos must be in [0,count);
void erase(unsigned int pos)
{
if(pos<count)
{
--count;
for(unsigned int i = pos;i<count;++i)
{
array[i] = array[i+1];
}
}
}

private:
T*  allocator(unsigned int size)
{
return new T[size];
}

void deallocator(T* arr)
{
if(arr)
delete[] arr;
}
private:
T*				array;
unsigned int	count;
unsigned int	allocCount;
};

#endif


//下面的是测试代码

void printfVector(myVector<int>& vector1)
{
for(unsigned int i = 0 ; i < vector1.size();++i)
{
cout<<vector1[i]<<",";
}
cout<<"alloc size = "<<vector1.allocSize()<<",size = "<<vector1.size()<<endl;
}

void main()
{
myVector<int> myVector1;
myVector<int> myVector2(0,10);
myVector2.push_front(1);
myVector2.erase(11);
printfVector(myVector2);
myVector1.push_back(2);
myVector1.push_front(1);
printfVector(myVector1);
myVector1.insert_after(1,3);
printfVector(myVector1);

myVector2 = myVector1;
myVector2.insert_before(0,0);
myVector2.insert_before(1,-1);
printfVector(myVector2);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: