您的位置:首页 > 其它

类模板模拟实现STL中Vector

2017-03-20 22:45 447 查看
vector(向量): C++中的一种数据结构,确切的说是一个类.它相当于一个动态的数组,当程序员无法知道自己需要的数组的规模多大时,用其来解决问题可以达到最大节约空间的目的.

用法:

1.变量声明:

例:声明一个int向量以替代一维的数组:vector a;(等于声明了一个int数组a[],大小没有指定,可以动态的向里面添加删除)。

例:用vector代替二维数组.其实只要声明一个一维数组向量即可,而一个数组的名字其实代表的是它的首地址,所以只要声明一个地址的向量即可,即:vector

#include <iostream>
using namespace std;
#include <assert.h>
template<typename T>
class Vector
{
public:
typedef T valueType;
typedef valueType* Iteator;
typedef const valueType* const_Iterator;
typedef valueType& Reference;
typedef const valueType& const_Reference;
typedef size_t size_type;

public:
Vector()   //构造函数
:_start(0)
,_finish(0)
,_end_of_storage(0)
{}

Vector(size_type n, const T& value = T())  //构造函数,构造一个里面有n个相同值的顺序表
:_start(new T
)
{
for(size_type idx=0; idx<n; ++idx)
_start[idx] = value;
_finish = _start+n;
_end_of_storage = _finish;
}

Vector(const Vector<T> &v)   //拷贝构造函数
{
size_type capacity = v._end_of_storage - v._start;
_start = new T[capacity];
size_type size = v._finish - v._start;

for(size_type idx=0; idx<size; ++idx)
{
_start[idx] = v._start[idx];
}
_finish = _start + size; //不能用_finish = v._finish;因为改变指向会引发错误
_end_of_storage = _start + capacity;
}

Vector<T>& operator = (const Vector<T>& v) //赋值运算符重载
{
if(this != &v)
{
size_type capacity = v._end_of_storage - v._start;
size_type size = v._finish - v._start;
if(Capacity() < size)
{
_start = new T[capacity];
for(size_type idx=0; idx<size; ++idx)
{
_start[idx] = v._start[idx];
}
_finish = _start + size; //不能用_finish = v._finish;因为改变指向会引发错误
_end_of_storage = _start + capacity;
}
}
return *this;
}

~Vector()   //析构函数
{
if(NULL != _start)
{
delete[] _start;
_start = NULL;
_finish = NULL;
_end_of_storage = NULL;
}
}

Iteator Begin()   //得到数组头的指针
{
return _start;
}

const_Iterator Begin()const
{
return _start;
}

Iteator End()  //得到数组的最后一个单元+1的指针
{
return _finish;
}

const_Iterator End()const
{
return _finish;
}

size_type Size()const   //当前使用数据的大小
{
return _finish - _start;
}

size_type Capacity()const   //当前vector分配的大小
{
return _end_of_storage - _start;
}

bool Empty()const  //判断vector是否为空
{
return Begin() == End();
}

Reference operator[](size_type index)  //得到编号位置的数据
{
assert(index<Size());
return _start[index];
}

const_Reference operator[](size_type index)const
{
assert(index<Size());
return _start[index];
}

Reference Front()   //得到数组头的引用
{
return *Begin();
}

Reference Back()   //得到数组的最后一个单元的引用
{
return *(End()-1);
}

void PushBack(const T& value)    //在数组的最后添加一个数据
{
CheckCapacity();
*_finish = value;
++_finish;
}

void PopBack()   //去掉数组的最后一个数据
{
assert(0 != Size());
--_finish;
}

Iteator Insert(Iteator pos, const T& value)  // 在指针指向元素的前面插入一个元素
{
size_type position = pos - Begin();
CheckCapacity();
int count = Size() - position ;
int i = 0;
while(count)
{
_start[Size()-i] = _start[Size()-i-1];
i++;
count--;
}
*pos = value;
_finish++;
return &(*pos);
}

Iteator Erase(Iteator pos)   //删除指针指向的数据项
{
size_type position = pos - Begin();
assert(0 != Size());
assert(position < Size());
size_t count = Size() - position - 1;
int i = 0;
while(count)
{
_start[position+i] = _start[position+i+1];
i++;
count--;
}
_finish--;

return pos;
}

void ReSize(size_type newSize, const T& value = T())  //重新设置该容器的大小
{
if(newSize < Size())
{
_finish -= (Size() - newSize);

}
else
{
size_t count = newSize - Size();
while(count)
{
CheckCapacity();
_start[Size()] = value;
++_finish;
count--;
}
}
}

void Assign(size_t n, const T& data)  //构造一个里面有n个相同值的顺序表
{
_finish = 0;
Iteator temp = new T
;

for(size_type idx=0; idx<n; ++idx)
_start[idx] = value;
_finish = _start+n;
_end_of_storage = _finish;
}

void Clear()const   //清空当前的vector
{
_finish = _start;
}

private:
void CheckCapacity()
{
if(_finish >= _end_of_storage)
{
int capacity = Capacity()*2 + 3;
Iteator pTemp = new T[capacity];
size_type size = _finish - _start;
memcpy(pTemp, _start, sizeof(T)*size);
if(_start != NULL)
{
delete[] _start;
}
_start = pTemp;
_finish = _start + size;
_end_of_storage = _start + capacity;
}
}

protected:
Iteator _start;
Iteator _finish;
Iteator _end_of_storage;
};

void FunTest1()
{
Vector<int> v1();
Vector<int> v2(10,4);
Vector<int> v3(v2);
Vector<int> v4;
v4 = v2;

cout<<v2.Capacity()<<endl;
cout<<v2.Size()<<endl;

Vector<int>::Iteator it = v2.Begin();
while(it != v2.End())
{
cout<<*it<<" ";
it++;
}
cout<<endl;

cout<<v4.Capacity()<<endl;
cout<<v4.Size()<<endl;
it = v4.Begin();
while(it != v4.End())
{
cout<<*it<<" ";
it++;
}
cout<<endl;
}

void FunTest2()
{
Vector<int> v1(3, 5);
v1.PushBack(1);
v1.PushBack(2);
v1.PushBack(3);
v1.PushBack(4);
v1.PushBack(5);
v1.PushBack(6);

cout<<v1.Size()<<endl;
cout<<v1.Capacity()<<endl;

v1.PopBack();
v1.PopBack();
v1.PopBack();
cout<<v1.Size()<<endl;
cout<<v1.Capacity()<<endl;

Vector<int>::Iteator it = v1.Begin();
while(it != v1.End())
{
cout<<*it<<" ";
it++;
}
cout<<endl;
}

void FunTest3()
{
Vector<int> v1(3, 5);
v1.PushBack(1);
v1.PushBack(2);
v1.PushBack(3);
v1.PushBack(4);
v1.PushBack(5);
v1.PushBack(6);

cout<<v1.Size()<<endl;
cout<<v1.Capacity()<<endl;
Vector<int>::Iteator it = v1.Begin();
while(it != v1.End())
{
cout<<*it<<" ";
it++;
}
cout<<endl;

v1.Erase(v1.Begin()+6);
v1.Insert(v1.Begin(), 10);
cout<<v1.Size()<<endl;
cout<<v1.Capacity()<<endl;

it = v1.Begin();
while(it != v1.End())
{
cout<<*it<<" ";
it++;
}
cout<<endl;

v1.Erase(v1.Begin()+2);
v1.Insert(v1.Begin(), 10);
cout<<v1.Size()<<endl;
cout<<v1.Capacity()<<endl;

it = v1.Begin();
while(it != v1.End())
{
cout<<*it<<" ";
it++;
}
cout<<endl;

v1.Insert(v1.Begin(), 10);
v1.Erase(v1.Begin());
cout<<v1.Size()<<endl;
cout<<v1.Capacity()<<endl;

it = v1.Begin();
while(it != v1.End())
{
cout<<*it<<" ";
it++;
}
cout<<endl;

}

void FunTest4()
{

Vector<int> v1(3, 5);
v1.PushBack(1);
v1.PushBack(2);
v1.PushBack(3);
v1.PushBack(4);
v1.PushBack(5);
v1.PushBack(6);
cout<<v1.Size()<<endl;
cout<<v1.Capacity()<<endl;

v1.ReSize(20,100);
cout<<v1.Size()<<endl;
cout<<v1.Capacity()<<endl;
}
int main()
{
FunTest1();
FunTest2();
FunTest3();
FunTest4();

system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息