#include<iostream>
using namespace std;
#include<assert.h>
namespace bit
{
template<class T>
class vector
{
public:
// Vector的迭代器是一个原生指针
typedef T* iterator;
typedef const T* const_iterator;
iterator begin()
{
return _start;
}
iterator end()
{
return _finish;
}
const_iterator cbegin()
{
return _start;
}
const_iterator cend() const
{
return _finish ;
}
// construct and destroy
vector()
{
_start = new T[2];
_finish = _start;
_endOfStorage = _start + 2;
}
vector(int n, const T& value = T())
{
_start = new T[n+1];
int i = 0;
for (i; i < n; i++)
{
_start[i] = value;
}
_finish = _start + i;
_endOfStorage = _finish;
}
template<class InputIterator>
vector(InputIterator first, InputIterator last)
{
int n = last - first + 1;
_start = new T[n+1];
for (int i = 0; i < n; i++)
{
_start[i] = first[i];
}
_finish = _start + n;
_endOfStorage = _finish;
}
vector(const vector<T>& v)
{
int n = v.size();
_start = new T[n+1];
for (int i = 0; i < n; i++)
{
_start[i] = v._start[i];
}
_finish = _start + n ;
_endOfStorage = _finish;
}
vector<T>& operator= (vector<T> v)
{
swap(v);
return *this;
}
~vector()
{
free(_start);
_start = _finish = _endOfStorage = nullptr;
}
// capacity
size_t size() const
{
return _finish - _start ;
}
size_t capacity() const
{
return _endOfStorage - _start;
}
void reserve(size_t n)
{
if (n > size())
{
int m = size();
int s = size() > 0 ? 2 : 2 * size() * sizeof(T);
iterator newn = new T[s];
int i = 0;
for (i; i < m; i++)
{
newn[i] = _start[i];
}
_start = newn;
_finish = _start + i ;
_endOfStorage = _start + s;
}
}
void resize(size_t n, const T& value = T())
{
if (n > size())
{
int m = size();
int s = size() > 0 ? 2 : 2 * size() * sizeof(T);
iterator newn = new T[s];
int i = 0;
for (i; i < m; i++)
{
newn[i] = _start[i];
}
for (i; i < s; i++)
{
newn[i] = value;
}
_start = newn;
_finish = _start + n ;
_endOfStorage = _start + s;
}
}
///access///
T& operator[](size_t pos)
{
return _start[pos];
}
const T& operator[](size_t pos)const
{
return _start[pos];
}
///modify/
void push_back(const T& x)
{
insert(_finish +1, x);
}
void pop_back()
{
assert(_finish != _start);
_finish--;
}
void swap(iterator a, iterator b)
{
iterator tmp = b;
b = a;
a = tmp;
}
void swap(vector<T>& v)
{
swap(_finish, v._finish);
swap(_start,v._start);
swap(_endOfStorage,v._endOfStorage);
}
iterator insert(iterator pos, const T& x)
{
if (_finish >= _endOfStorage)
{
int m = size();
int k = pos - _start;
int s = 2 * capacity() * sizeof(T);
iterator newn = new T[s];
int i = 0;
for (i; i < m; i++)
{
newn[i] = _start[i];
}
_start = newn;
_finish = _start + i ;
pos = _start + k;
_endOfStorage = _start + s;
}
if (pos > _finish)
{
*_finish = x;
}
else
{
iterator t = _finish ;
while (t > pos)
{
*t = *(t - 1);
t--;
}
*t = x;
}
_finish++;
return pos;
}
iterator erase(iterator pos)
{
iterator k = pos;
while (k != _finish)
{
*k = *(k + 1);
k++;
}
_finish--;
return pos;
}
private:
iterator _start; // 指向数据块的开始
iterator _finish; // 指向有效数据的尾
iterator _endOfStorage; // 指向存储容量的尾
};
}
vector模拟实现
最新推荐文章于 2024-11-04 16:58:15 发布