#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <algorithm>
namespace my
{
template<class T>
class vector
{
public:
typedef T* iterator;
typedef const T* const_iterator;
private:
iterator _start;
iterator _finish;
iterator _endofstorage;
public:
vector()
:_start(nullptr),
_finish(nullptr),
_endofstorage(nullptr)
{
}
vector(const vector<T>& x)
:_start(nullptr),
_finish(nullptr),
_endofstorage(nullptr)
{
//传统写法
//size_t sz = x.size();
//T* tmp = new T[x.capacity()];
//memmove(tmp, x._start, sz * sizeof(T));
//_start = tmp;
//_finish = _start + sz;
//_endofstorage = _start + x.capacity();
//简化写法
vector<int> tmp(x._start, x._finish);
swap(tmp);
}
template<class InputIterator>
vector(InputIterator first, InputIterator last)
:_start(nullptr),
_finish(nullptr),
_endofstorage(nullptr)
{
while (first != last)
{
push_back(*first);
first++;
}
}
~vector()
{
delete[] _start;
}
size_t size() const
{
return _finish - _start;
}
size_t capacity() const
{
return _endofstorage - _start;
}
bool empty()
{
return _start == _finish;
}
iterator begin()
{
return _start;
}
iterator end()
{
return _finish;
}
void reserve(size_t n)
{
T* tmp = new T[n];
int len = size();
for (int i = 0; i < len; i++)
{
tmp[i] = _start[i];
}
//使用memmove,在存储string这种对象时,会引起浅拷贝问题
//memmove(tmp, _start, sizeof(T) * len);
delete[] _start;
_start = tmp;
_finish = _start + len;
_endofstorage = _start + n;
}
void resize(size_t n, const T& x = T())
{
if (n <= size())
{
_finish = _start + n;
}
else if (n <= capacity())
{
while (_finish < _endofstorage)
{
*_finish = x;
_finish++;
}
}
else
{
int sz = size();
T* tmp = new T[n];
memmove(tmp, _start, sz * sizeof(T));
_start = tmp;
_finish = _start + sz;
_endofstorage = _start + n;
while (_finish < _endofstorage)
{
*_finish = x;
_finish++;
}
}
}
void swap(vector<T>& v)
{
std::swap(_start, v._start);
std::swap(_endofstorage, v._endofstorage);
std::swap(_finish, v._finish);
}
T& operator[](const size_t pos)
{
assert(pos < size());
return _start[pos];
}
const T& operator[](const size_t pos) const
{
assert(pos < size());
return _start[pos];
}
vector<T>& operator=(const vector<T>& v)
{
if (this != &v)
{
vector<T> tmp(v);
swap(tmp);
}
return *this;
}
void push_back(const T& x)
{
if (_finish == _endofstorage)
{
//扩容
size_t newCapacity = capacity() == 0 ? 4 : capacity() * 2;
reserve(newCapacity);
}
*_finish = x;
++_finish;
}
void pop_back()
{
assert(!empty());
_finish--;
}
iterator insert(iterator pos, const T& x)
{
assert(pos >= begin() && pos <= _finish);
if (_finish == _endofstorage)
{
int len = pos - _start;
size_t newCapacity = capacity() == 0 ? 4 : capacity() * 2;
reserve(newCapacity);
pos = _start + len;
}
iterator end = _finish - 1;
while (end >= pos)
{
*(end + 1) = *end;
end--;
}
end++;
*end = x;
_finish++;
return end;
}
iterator erase(iterator pos)
{
assert(pos >= _start && pos < _finish);
iterator cur = pos;
while (cur < _finish - 1)
{
*cur = *(cur + 1);
cur++;
}
_finish--;
return pos;
}
void print()
{
for (auto i : *this)
{
cout << i << ' ';
}
cout << endl;
}
};
}
简单实现STL vector
最新推荐文章于 2023-10-08 15:01:18 发布