#pragma once
#include<assert.h>
namespace lhw
{
template<class T>
class vector
{
typedef T* iterator;
typedef const T* const_iterator;
public:
vector()
:_start(nullptr)
,_finish(nullptr)
,_end_of_storage(nullptr)
{}
vector(size_t n,const T& val)
:_start(nullptr)
, _finish(nullptr)
, _end_of_storage(nullptr)
{
reserve(n);
for (size_t i = 0; i < n; i++)
{
push_back(val);
}
}
template<class InputIterator>
vector(InputIterator first, InputIterator last)
:_start(nullptr)
, _finish(nullptr)
, _end_of_storage(nullptr)
{
while (last != first)
{
push_back(*first);
first++;
}
}
vector(const vector<T>& v)
{
_start = new T[v.capacity()];
for (size_t i = 0; i < v.size(); i++)
{
_start[i] = v._start[i];
}
_finish = _start + v.size();
_end_of_storage = _start + v.capacity();
}
vector<T>& operator=(const vector<T>& v)
{
if (this != &v)
{
delete[] _start;
_str = new T[v.capacity()];
for (size_t i = 0; i < v.size(); i++)
{
_str[i] = v._start[i];
}
_finish = _start + v.size();
_end_of_storage = _start + v.capacity();
}
return *this;
}
vector<T>& operator=(const vector<T>& v)
{
swap(v);
return *this;
}
~vector()
{
if (_start)
{
delete[] _str;
_start = _finish = _end_of_storage = nullptr;
}
}
iterator begin()
{
return _start;
}
const_iterator begin()const
{
return _start;
}
iterator end()
{
return _finish;
}
const_iterator end()const
{
return _finish;
}
size_t size()
{
return _finish - _start;
}
size_t capacity()
{
return _end_of_storage - _start;
}
void reserve(size_t n)
{
if (n > capacity())
{
T* tmp = new T[n];
size_t sz = size();
if (_start)
{
for (size_t i = 0; i < size(); i++)
{
tmp[i] = _start[i];
}
delete[] _start;
}
_start = tmp;
_finish = _start + sz;
_end_of_storage = _start + n;
}
}
void resize(size_t n, T& val)
{
if (n < size())
{
_finsih = _start + n;
}
else
{
if (n > capacity())
{
reserve(n);
}
while (_finish < _start + n)
{
*_finish = val;
_finish++;
}
}
}
bool empty()
{
return _start == _finish;
}
void push_back(const T& val)
{
if (_start == _finish)
{
size_t newcapacity = capacity() == 0 ? 4 : 2 * capacity();
reserve(newcapacity);
}
*_finish = val;
_finish++;
}
void pop_back()
{
assert(_start == _finish);
_finish--;
}
void insert(iterator pos, const T& val)
{
assert(pos <= _finish);
if (_finish == _end_of_storage)
{
size_t len = pos - _start;
size_t newcapacity = capacity() == 0 ? 4 : 2 * capacity();
reserve(newcapacity);
pos = _start + len;
}
iterator end = _finish - 1;
while (end >= pos)
{
*(end + 1) = *end;
end--;
}
*pos = val;
_finish++;
}
iterator erase(iterator pos)
{
assert(!empty());
iterator it = pos;
while (it + 1 != _finish)
{
*it = *(it + 1);
it++;
}
_finish--;
return pos;
}
void swap(vector<T>& v)
{
::swap(_start, v._start);
::swap(_finish, v._finish);
::swap(_end_of_storage, v._end_of_storage);
}
T& operator[](size_t i)
{
assert(i < size());
return _start[i];
}
const T& operator[](size_t i)const
{
assert(i < size());
return _start[i];
}
private:
iterator _start;
iterator _finish;
iterator _end_of_storage;
};
}
STL中vector的模拟实现
最新推荐文章于 2024-09-10 19:16:08 发布