目录
一、模拟实现的思路
【大概思路】
利用_start和_finish来确定vector的位置和所存入数据数目,利用_capacity来确定vector的整体容量大小。后续再进行相关函数功能的实现。
二、逐步模拟实现
1.成员变量的设置
namespace imitate
{
template<class T>
class vector
{
public:
typedef T* const_iterator;
typedef T* iterator;
public:
// ... 后续的成员函数
private:
iterator _start;
iterator _finish;
iterator _endofstorage;
};
}
2.迭代器的相关函数
iterator begin()
{
return _start;
}
iterator end()
{
return _finish;
}
const_iterator begin() const
{
return _start;
}
const_iterator end() const
{
return _finish;
}
3.构造函数
【注意】
memcpy,strcpy第一次拷贝的时候不能给空,memcpy不能使用进行对象之间的拷贝,是按字节拷贝的浅拷贝。
vector()
: _start(nullptr)
, _finish(nullptr)
, _endofstorage(nullptr)
{}
vector(const vector<T>& v)
{
_start = new T[v.capacity()];
_finish = _start;
_endofstorage = _start + v.capacity();
//memcpy(tmp, _start, sizeof(T)*size());
//memcpy,strcpy第一次拷贝的时候不能给空
//memcpy 不能使用进行对象之间的拷贝,是按字节拷贝的浅拷贝
for (int i = 0; i < v.size(); ++i)
{
*_finish = v[i];
_finish++;
}
}
vector(iterator begin, iterator end)
{
size_t n = end - begin;
_start = new T[n];
_endofstorage = _start + n;
_finish = _start;
for (int i = 0; i < n; ++i)
{
*_finish = begin[i];
++_finish;
}
}
~vector()
{
delete[] _start;
_start = _finish = _endofstorage = nullptr;
}
4.赋值操作
vector<T>& operator=(vector<T>& v)
{
if (this != &v)
{
vector<T> tmp(v);
swap(tmp);
}
return *this;
}
void assign(iterator begin, iterator end)
{
vector<T> tmp(begin, end);
swap(tmp);
}
void assign(size_t n, const T& x)
{
if(!_start) delete[] _start;
iterator tmp = new T[n];
for (size_t i = 0; i < n; ++i) tmp[i] = x;
_start = tmp;
_finish = _start + n;
_endofstorage = _start + n;
}
5.容量与大小
size_t size() const
{
return _finish - _start;
}
size_t capacity() const
{
return _endofstorage - _start;
}
bool empty() const
{
return (_finish - _start == 0);
}
void reserve(size_t n)
{
if (n <= capacity()) return;
size_t len = size();
iterator tmp = new T[n];
if (_start)
{
for (size_t i = 0; i < len; ++i)
{
tmp[i] = _start[i];
}
delete[] _start;
}
_start = tmp;
_finish = tmp + len;
_endofstorage = tmp + n;
}
void resize(size_t n, const T& elem = T())
{
if (n <= size())
{
_finish = _start + n;
return;
}
if (n >= capacity())
{
reserve(n);
}
iterator it = _finish;
_finish += n;
while (it != _finish)
{
*it = elem;
it++;
}
}
6.数据存取
T& operator[](size_t i)
{
return _start[i];
}
T& operator[](size_t i) const
{
return _start[i];
}
7.互换容器
void swap(vector<T>& v)
{
std::swap(_start, v._start);
std::swap(_finish, v._finish);
std::swap(_endofstorage, v._endofstorage);
}
8.插入与删除
注意插入与删除会改变原有迭代器指向的位置,需要及时更新迭代器。
void push_back(const T& x)
{
if (_finish == _endofstorage)
{
size_t newcapacity = capacity() == 0 ? 2 : capacity() * 2;
reserve(newcapacity);
}
*_finish = x;
_finish++;
}
void pop_back()
{
assert(_start < _finish);
_finish--;
}
iterator insert(iterator pos, const T& x)
{
assert(pos < _finish);
if (_finish == _endofstorage)
{
size_t n = pos - _start; // 防止迭代器失效,先储存元素个数
size_t newcapacity = (capacity() == 0) ? 2 : capacity() * 2;
reserve(newcapacity);
pos = _start + n; // 防止迭代器失效,即使更新pos的地址
}
iterator end = _finish;
while (end > pos)
{
*end = *(end - 1);
end--;
}
*pos = x;
_finish++;
return pos;
}
void insert(iterator pos, size_t n, const T& x)
{
assert(pos < _finish);
if (_endofstorage - _finish < n)
{
size_t n = pos - _start;
size_t newcapacity = (capacity() == 0) ? n : size() + n;
reserve(newcapacity);
pos = _start + n;
}
iterator it1 = _finish + n;
while (it1 < pos + n)
{
*it1 = *(it1 - 1);
it1--;
}
for (iterator it2 = pos; it2 != pos + n; ++it2)
{
*it2 = x;
}
_finish += n;
}
iterator erase(const_iterator pos)
{
assert(pos < _finish);
iterator it = pos;
while (it < _finish)
{
*it = *(it + 1);
it++;
}
_finish--;
return pos;
}
iterator erase(iterator begin, iterator end)
{
assert(begin < _finish && end <= _finish);
size_t n = end - begin;
iterator it = begin;
while (it + n < _finish)
{
*it = *(it + n);
it++;
}
_finish -= n;
return begin;
}
三、总结
MyVector.h
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<assert.h>
namespace imitate
{
template<class T>
class vector
{
public:
typedef T* const_iterator;
typedef T* iterator;
public:
vector()
: _start(nullptr)
, _finish(nullptr)
, _endofstorage(nullptr)
{}
vector(const vector<T>& v)
{
_start = new T[v.capacity()];
_finish = _start;
_endofstorage = _start + v.capacity();
//memcpy(tmp, _start, sizeof(T)*size());
//memcpy,strcpy第一次拷贝的时候不能给空,memcpy不能使用进行对象之间的拷贝,是按字节拷贝的浅拷贝
for (int i = 0; i < v.size(); ++i)
{
*_finish = v[i];
_finish++;
}
}
vector(iterator begin, iterator end)
{
size_t n = end - begin;
_start = new T[n];
_endofstorage = _start + n;
_finish = _start;
for (int i = 0; i < n; ++i)
{
*_finish = begin[i];
++_finish;
}
}
~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);
}
void reserve(size_t n)
{
if (n <= capacity()) return;
size_t len = size();
iterator tmp = new T[n];
if (_start)
{
for (size_t i = 0; i < len; ++i)
{
tmp[i] = _start[i];
}
delete[] _start;
}
_start = tmp;
_finish = tmp + len;
_endofstorage = tmp + n;
}
void resize(size_t n, const T& elem = T())
{
if (n <= size())
{
_finish = _start + n;
return;
}
if (n >= capacity())
{
reserve(n);
}
iterator it = _finish;
_finish += n;
while (it != _finish)
{
*it = elem;
it++;
}
}
void push_back(const T& x)
{
if (_finish == _endofstorage)
{
size_t newcapacity = capacity() == 0 ? 2 : capacity() * 2;
reserve(newcapacity);
}
*_finish = x;
_finish++;
}
void pop_back()
{
assert(_start < _finish);
_finish--;
}
iterator insert(iterator pos, const T& x)
{
assert(pos < _finish);
if (_finish == _endofstorage)
{
size_t n = pos - _start; // 防止迭代器失效,先储存元素个数
size_t newcapacity = (capacity() == 0) ? 2 : capacity() * 2;
reserve(newcapacity);
pos = _start + n; // 防止迭代器失效,即使更新pos的地址
}
iterator end = _finish;
while (end > pos)
{
*end = *(end - 1);
end--;
}
*pos = x;
_finish++;
return pos;
}
void insert(iterator pos, size_t n, const T& x)
{
assert(pos < _finish);
if (_endofstorage - _finish < n)
{
size_t n = pos - _start;
size_t newcapacity = (capacity() == 0) ? n : size() + n;
reserve(newcapacity);
pos = _start + n;
}
iterator it1 = _finish + n;
while (it1 < pos + n)
{
*it1 = *(it1 - 1);
it1--;
}
for (iterator it2 = pos; it2 != pos + n; ++it2)
{
*it2 = x;
}
_finish += n;
}
iterator erase(const_iterator pos)
{
assert(pos < _finish);
iterator it = pos;
while (it < _finish)
{
*it = *(it + 1);
it++;
}
_finish--;
return pos;
}
iterator erase(iterator begin, iterator end)
{
assert(begin < _finish && end <= _finish);
size_t n = end - begin;
iterator it = begin;
while (it + n < _finish)
{
*it = *(it + n);
it++;
}
_finish -= n;
return begin;
}
vector<T>& operator=(vector<T>& v)
{
if (this != &v)
{
vector<T> tmp(v);
swap(tmp);
}
return *this;
}
void assign(iterator begin, iterator end)
{
vector<T> tmp(begin, end);
swap(tmp);
}
void assign(size_t n, const T& x)
{
if(!_start) delete[] _start;
iterator tmp = new T[n];
for (size_t i = 0; i < n; ++i) tmp[i] = x;
_start = tmp;
_finish = _start + n;
_endofstorage = _start + n;
}
iterator begin()
{
return _start;
}
iterator end()
{
return _finish;
}
const_iterator begin() const
{
return _start;
}
const_iterator end() const
{
return _finish;
}
T& operator[](size_t i)
{
return _start[i];
}
T& operator[](size_t i) const
{
return _start[i];
}
size_t size() const
{
return _finish - _start;
}
size_t capacity() const
{
return _endofstorage - _start;
}
bool empty() const
{
return (_finish - _start == 0);
}
private:
iterator _start;
iterator _finish;
iterator _endofstorage;
};
}