vector和数组类似,拥有一段连续的内存空间,并且起始地址不变。
因此能高效的进行随机存取,时间复杂度为o(1);
但因为内存空间是连续的,所以在进行插入和删除操作时,会造成内存块的拷贝,时间复杂度为o(n)。
另外,当数组中内存空间不够时,会重新申请一块内存空间并进行内存拷贝。
#ifndef TVECTOR_H
#define TVECTOR_H
#include <stdlib.h>
#include <ASSERT.H>
#include <string.h>
#include <QDebug>
typedef unsigned int uint;
class iterator;
using namespace std;
//指定地址new new (d->end()) T(t)
//memmove
//realloc
template <class T>
class TVector
{
public:
TVector()
{
nCapacity = 16;
pData = (T*)malloc(sizeof(T)*nCapacity);
//qDebug()<<"pData:"<<(int)pData;
nSize = 0;
}
class iterator
{
public:
T* i;
inline iterator(): i(nullptr) {}
inline iterator(T* n)
{
//qDebug()<<"iterator:"<<(int)n;
i = n;
}
inline iterator(const iterator &o): i(o.i){}
inline T &operator*() const { return *i; }
inline T *operator->() const { return i; }
inline bool operator==(const iterator &o) const { return i == o.i; }
inline bool operator!=(const iterator &o) const { return i != o.i; }
inline iterator operator+(int j) const { return i+j; }
inline iterator operator-(int j) const { return i-j; }
inline int operator-(iterator j) const { return i - j.i; }
//++i
inline iterator &operator++() { ++i; return *this; }
//i++
inline iterator operator++(int) {
//node *n = i; ++i; return n;
iterator tmp = *this;++(*this);return tmp;}
//--i
inline iterator &operator--() { --i; return *this; }
//i--
inline iterator operator--(int) { iterator tmp = *this;--(*this);return tmp; }
};
//传回索引idx所指的数据,如果idx越界,抛出out_of_range
T at(uint index)
{
assert(index <= nSize);
return *(pData)[index];
}
//传回最后一个数据,不检查这个数据是否存在
T back()
{
return *(end()-1);
}
//传回迭代器第一个数据
iterator begin()
{
//qDebug()<<"begin:"<<(int)pData;
return pData;
}
//返回容器中容量个数。
uint capacity()
{
return nCapacity;
}
//移除容器中所有数据。
void clear()
{
memset(pData,0,nCapacity*sizeof(T));
nSize = 0;
}
//判断容器是否为空。
bool empty()
{
return nSize == 0;
}
//指向迭代器中的最后一个数据地址。
iterator end()
{
iterator ret = (T*)(pData+nSize);
//qDebug()<<"end:"<<(int)ret.i;
return ret;
}
//删除pos位置的数据,传回下一个数据的位置。
void erase(iterator position)
{
if(position != end())
{
int temp = position;
memmove((void*)position,(void*)temp++,(int)end() - (int)temp);
}
}
//传回第一个数据。
T front()
{
return *begin();
}
//在pos位置插入一个elem拷贝,传回新数据位置。
iterator insert(iterator pos,T& elem)
{
reallocMemery();
//qDebug()<<"pData:"<<(int)pData;
//移动pos后面的数据
memmove((T*)(pos.i+1),(T*)pos.i,(end() - pos)*sizeof(T));
//赋值
//qDebug()<<"pos.i:"<<(int)pos.i;
*(pos.i) = elem;
nSize++;
return ++pos;
}
//返回容器中最大数据的数量。
uint max_size()
{
return nCapacity;
}
//删除最后一个数据。
void pop_back()
{
if(nSize > 0)
{
nSize--;
}
}
//在尾部加入一个数据。
void push_back(const T& elem)
{
reallocMemery();
T temp = elem;
insert(end(),temp);
}
//重新指定队列的长度。
void resize(uint num)
{
reallocMemery(num);
}
//保留适当的容量。
void reserve()
{
//todo
//修改capacity,然后进行数据操作
}
//返回容器中实际数据的个数。
uint size()
{
return nSize;
}
//重载下标操作符
T operator[](int index)
{
assert(nSize >= index);
return *(pData+index);
}
private:
T* pData;
unsigned int nCapacity;
unsigned int nSize;
void reallocMemery(int n = -1)
{
//resize
if(n >= 0)
{
if((nCapacity - n) > 1)
{
nSize = n;
}
else
{
while ((nCapacity - n) <= 1)
{
nCapacity = nCapacity * 2;
}
char* result = (char*)realloc(pData,nCapacity * sizeof(T));
assert(result);
int count = n - nSize;
for(int i = 0; i < count;i++)
{
T tempT;
*(pData+nSize + i) = tempT;
}
nSize = n;
}
return;
}
//普通增加元素
if((nCapacity - nSize) <= 1 )
{
nCapacity = nCapacity * 2;
char* result = (char*)realloc(pData,nCapacity * sizeof(T));
assert(result);
memset(pData + nCapacity * sizeof(T),0,nCapacity * sizeof(T));
}
}
};
#endif // TVECTOR_H