自学内容网 自学内容网

vector的模拟实现(C++)

一、构造函数

vector()   //构造函数
:_start(nullptr)
,_finish(nullptr)
,_endofstorage(nullptr)
{}

vector(int n, const T& val = T())//构造函数
:_start(nullptr)
, _finish(nullptr)
, _endofstorage(nullptr)
{
reserve(n);
while (n--)
{
push_back(val);
}
}

template<class InputIterator>
vector(InputIterator first, InputIterator last)//构造函数
:_start(nullptr)
, _finish(nullptr)
, _endofstorage(nullptr)
{
while (first != last)
{
push_back(*first);
first++;
}
}

 二、拷贝构造

vector(const vector<T>& v)//拷贝构造
:_start(nullptr)
,_finish(nullptr)
,_endofstorage(nullptr)
{
reserve(v.capacity());
for (auto& c : v)
{
push_back(c);
}
}

三、析构

~vector()
{
delete[] _start;
_start = _finish = _endofstorage = nullptr;
}

四、重载

void swap(vector<T>& v)
{
std:: swap(_start, v._start);
std::swap(_finish, v._finish);
std::swap(_endofstorage, v._endofstorage);
}
vector<T>& operator=(vector<T> tmp)
{
swap(tmp);
return *this;
}
T& operator[](size_t pos)
{
assert(pos < size());
return *(_start + pos);
}
const T& operator[](size_t pos) const
{
assert(pos < size());
return *(_start + pos);
}

五、扩容及增删

size_t size() const
{
return _finish - _start;
}

size_t capacity() const
{
return _endofstorage - _start;
}

void push_back(const T& val)
{
//if (_finish == _endofstorage)
//{
//reserve(capacity() == 0 ? 4 : capacity() * 2);
//}
//*_finish = val;
//_finish++;

insert(end(), val);
}

void reserve(size_t n)
{
if (capacity() < n)
{
size_t sz = size();
T* tmp = new T[n];
if (_start)
{
for (size_t i = 0; i < sz; i++)
{
tmp[i] = _start[i];
}
delete[] _start;
}
_start = tmp;
_finish = _start + sz;
_endofstorage = _start + n;
}
}

void resize(size_t n, const T& val = T())
{
if (n <= size())
{
_finish = _start + n;
}
else
{
reserve(n);
while (_finish != _start + n)
{
*_finish = val;
_finish++;
}
}
}
void pop_back()
{
_finish--;
}
void insert(iterator pos, const T& x)
{
assert(pos >= _start);
assert(pos <= _finish);
if (_finish == _endofstorage)
{
size_t len = pos - _start;
reserve(capacity() == 0 ? 4 : capacity() * 2);
pos = _start + len;
}

iterator end = _finish - 1;
while (end >= pos)
{
*(end + 1) = *end;
end--;
}
*pos = x;
_finish++;
}

void erase(iterator pos)
{
assert(pos >= _start);
assert(pos < _finish);
while (pos != end())
{
*pos = *(pos + 1);
pos++;
}
_finish--;
}

总体:

namespace asd //设一个命名空间避免与std冲突
{
template<class T>
class vector
{
public:
typedef T* iterator;
typedef const T* const_iterator;
vector()   //构造函数
:_start(nullptr)
,_finish(nullptr)
,_endofstorage(nullptr)
{}

vector(int n, const T& val = T())//构造函数
:_start(nullptr)
, _finish(nullptr)
, _endofstorage(nullptr)
{
reserve(n);
while (n--)
{
push_back(val);
}
}

template<class InputIterator>
vector(InputIterator first, InputIterator last)//构造函数
:_start(nullptr)
, _finish(nullptr)
, _endofstorage(nullptr)
{
while (first != last)
{
push_back(*first);
first++;
}
}

vector(const vector<T>& v)//拷贝构造
:_start(nullptr)
,_finish(nullptr)
,_endofstorage(nullptr)
{
reserve(v.capacity());
for (auto& c : v)
{
push_back(c);
}
}

vector<T>& operator=(vector<T> tmp)
{
swap(tmp);
return *this;
}

~vector()
{
delete[] _start;
_start = _finish = _endofstorage = nullptr;
}

        //迭代器
iterator begin()
{
return _start;
}

iterator end()
{
return _finish;
}

const_iterator cbegin() const
{
return _start;
}

const_iterator cend() const
{
return _finish;
}

size_t size() const
{
return _finish - _start;
}

size_t capacity() const
{
return _endofstorage - _start;
}

void push_back(const T& val)
{
//if (_finish == _endofstorage)
//{
//reserve(capacity() == 0 ? 4 : capacity() * 2);
//}
//*_finish = val;
//_finish++;

insert(end(), val);
}

void reserve(size_t n)
{
if (capacity() < n)
{
size_t sz = size();
T* tmp = new T[n];
if (_start)
{
for (size_t i = 0; i < sz; i++)
{
tmp[i] = _start[i];
}
delete[] _start;
}
_start = tmp;
_finish = _start + sz;
_endofstorage = _start + n;
}
}

void resize(size_t n, const T& val = T())
{
if (n <= size())
{
_finish = _start + n;
}
else
{
reserve(n);
while (_finish != _start + n)
{
*_finish = val;
_finish++;
}
}
}

T& operator[](size_t pos)
{
assert(pos < size());
return *(_start + pos);
}

const T& operator[](size_t pos) const
{
assert(pos < size());
return *(_start + pos);
}

void pop_back()
{
_finish--;
}

void swap(vector<T>& v)
{
std:: swap(_start, v._start);
std::swap(_finish, v._finish);
std::swap(_endofstorage, v._endofstorage);
}

void insert(iterator pos, const T& x)
{
assert(pos >= _start);
assert(pos <= _finish);
if (_finish == _endofstorage)
{
size_t len = pos - _start;
reserve(capacity() == 0 ? 4 : capacity() * 2);
pos = _start + len;
}

iterator end = _finish - 1;
while (end >= pos)
{
*(end + 1) = *end;
end--;
}
*pos = x;
_finish++;
}

void erase(iterator pos)
{
assert(pos >= _start);
assert(pos < _finish);
while (pos != end())
{
*pos = *(pos + 1);
pos++;
}
_finish--;
}
private:
iterator _start;
iterator _finish;
iterator _endofstorage;
};
}


原文地址:https://blog.csdn.net/N_N12/article/details/145100080

免责声明:本站文章内容转载自网络资源,如侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!