Vector重写
#pragma once
// 参考自: https://www.cnblogs.com/xslwm/p/7966563.html
#define VECTOR_NEW_UNIT_SIZE 1024
#include <iostream>
using namespace std;
template<class T>
class CVectorEx
{
public:
//迭代器
struct MyIterator
{
T* pt;
MyIterator(void) { pt = NULL; }
MyIterator(MyIterator& it) { pt = it.pt; }
~MyIterator(void) { pt = NULL; }
MyIterator operator=(const MyIterator& it)
{
pt = it.pt;
return *this;
}
bool operator!=(const MyIterator& it)
{
return (pt != it.pt);
}
T operator*(void)
{
return (*pt);
}
MyIterator& operator++(void)
{
++pt;
return *this;
}
MyIterator operator++(int)
{
MyIterator tmp = *this;
++pt;
return tmp;
}
MyIterator operator+(int n)const
{
MyIterator tmp;
tmp.pt = pt + n;
return tmp;
}
int operator-(const MyIterator& it)const
{
return (pt - it.pt);
}
MyIterator operator-(int n)
{
MyIterator tmp;
tmp.pt = pt - n;
return tmp;
};
};
CVectorEx(void) { buff = NULL; len = maxSize = 0; }
~CVectorEx(void) { _clear(); }
size_t capacity(void);
size_t size(void);
bool empty(void);
void print(void);
MyIterator begin(void)
{
MyIterator tmp;
tmp.pt = buff + 0;
return tmp;
}
MyIterator end(void)
{
MyIterator tmp;
tmp.pt = buff + len;
return tmp;
}
T operator[](size_t pos)
{
return *(buff + pos);
}
void push_back(T t);
void insert(const MyIterator& it, T t);
void insert(const MyIterator& it, const MyIterator& first, const MyIterator& last);
void assign(const MyIterator& first, const MyIterator& last);
T at(size_t pos);
private:
T* buff;
size_t len;
size_t maxSize;
void _clear();
};
template<class T>
void CVectorEx<T>::push_back(T t)
{
if (len >= maxSize)
{
maxSize += ((maxSize >> 1) > 1) ? (maxSize >> 1) : 1;
T* tmp = new T[maxSize];
if (buff)
{
memcpy(tmp, buff, sizeof(T)*len);
delete[] buff;
}
buff = tmp;
}
buff[len++] = t;
}
template<class T>
void CVectorEx<T>::insert(const MyIterator& it, T t)
{
int pos = it - begin();
if (pos > (int)len)
return;
if (len >= maxSize)
maxSize += ((maxSize >> 1) > 1) ? (maxSize >> 1) : 1;
T* tmp = new T[maxSize];
if (buff) {
memcpy(tmp, buff, sizeof(T)*pos);
tmp[pos] = t;
memcpy(tmp + pos + 1, buff + pos, sizeof(T)*(len - pos));
delete[] buff;
}
else
tmp[pos] = t;
buff = tmp;
++len;
}
template<class T>
void CVectorEx<T>::insert(const MyIterator& it, const MyIterator& first, const MyIterator& last)
{
int pos = it - begin();
if (pos > (int)len)
return;
int count = 0;
if ((count = last - first) <= 0)
return;
while ((len + count) >= maxSize)
maxSize += ((maxSize >> 1) > 1) ? (maxSize >> 1) : 1;
T* tmp = new T[maxSize];
if (buff) {
memcpy(tmp, buff, sizeof(T)*pos);
for (int i = 0; i < count; ++i)
tmp[pos + i] = *(first + i);
memcpy(tmp + pos + count, buff + pos, sizeof(T)*(len - pos));
delete[] buff;
}
else
for (int i = 0; i < count; ++i)
tmp[pos + i] = *(first + i);
buff = tmp;
len += count;
}
template<class T>
void CVectorEx<T>::assign(const MyIterator& first, const MyIterator& last)
{
_clear();
insert(begin(), first, last);
}
template<class T>
T CVectorEx<T>::at(size_t pos)
{
return *(buff + pos);
}
template<class T>
void CVectorEx<T>::_clear()
{
if (buff)
delete[] buff;
buff = NULL;
len = maxSize = 0;
}
template<class T>
size_t CVectorEx<T>::capacity()
{
return maxSize;
}
template<class T>
size_t CVectorEx<T>::size()
{
return len;
}
template<class T>
bool CVectorEx<T>::empty()
{
return (len == 0);
}
template<class T>
void CVectorEx<T>::print()
{
/*CVectorEx<T>::MyIterator it;
it = begin();
while(it != end())
cout << *it++ << " ";*/
for (auto it = begin(); it != end(); ++it)
{
cout << *it << " ";
}
cout << endl;
}