c++系列之vector类模拟实现
💗 💗 博客:小怡同学
💗 💗 个人简介:编程小萌新
💗 💗 如果博客对大家有用的话,请点赞关注再收藏 🌞
构造函数
vector()
//_begin表示有效成员的开始
//_finish表示有效成员的大小
//_end表示有效的容积
vector()
:_begin(nullptr)
,_finish(nullptr)
,_end(nullptr)
{
}
vector(size_t n, const T& value = T())
//这里不用memcpy拷贝的原因是
//1. memcpy是内存的二进制格式拷贝,将一段内存空间中内容原封不动的拷贝到另外一段内存空间中
//2. 如果拷贝的是内置类型的元素,memcpy既高效又不会出错,但如果拷贝的是自定义类型元素,并且自定义类型元素中涉及到资源管理时,就会出错,因为memcpy的拷贝实际是浅拷贝。
//3.内置类型也有拷贝构造
vector(int n, const T& value = T())
:_begin(nullptr)
, _finish(nullptr)
, _end(nullptr)
{
_begin = new T[n];
_finish = _begin + n;
_end = _begin + n;
for (int i = 0; i < n; i++)
{
*(_begin + i) = value;
}
}
//便捷写法
/*vector(int n, const T& value = T())
{
reserve(n);
for (int i = 0; i < n; i++)
{
_begin[i] = value;
}
}
vector(int n, const T& value = T())
{
resize(n,value);
}*/
vector(InputIterator first, InputIterator last)
//迭代器构造
template<class InputIterator>
vector(InputIterator first, InputIterator last)
{
while (first!= last)
{
push_back(*first);//尾插
first++;
}
}
vector(vector& v)
//拷贝构造
vector(vector<T>& v)
{
size_t size = v._finish - v._begin;
size_t capacity = v. _end - v._begin;
_begin = new T[capacity];
for (size_t i = 0; i < size; i++)
{
*(_begin + i) = *(v._begin + i);
}
_finish = _begin + size;
_end = _begin + capacity;
}
//简便写法
vector(vector<T>& v)
{
vector<T> tmp(v.begin(),v.end());
swap(tmp);
}
析构函数
~vector()
~vector()
{
if (_begin)
{
delete[] _begin;
_begin = _end = _finish = nullptr;
}
}
运算符重载
vector& operator = (vector v)
//赋值拷贝
vector<T>& operator = (vector<T> v)
{
swap(v);//这里传的是v的拷贝,改变v并不影响原本的对象
return *this;
}
T& operator[](size_t pos);
T& operator[](size_t pos)
{
assert(pos >= 0 && pos < size());//pos在有效范围种
return _begin[pos];
}
//const对象调用
const T& operator[](size_t pos)const
{
assert(pos >= _begin && pos < _finish);
return _begin[pos];
}
查询vector容积和大小
size_t size()
size_t capacity()
size_t size() const
{
return _finish - _begin;
}
size_t capacity() const
{
return _end - _begin;
}
size_t size()
{
return _finish - _begin;
}
size_t capacity()
{
return _end - _begin;
}
调整vector的容积和大小
void reserve(size_t n)
void resize(size_t n, const T& value = T())
//这里发生扩容之后,原本的_begin,_finish,_end会发生改变,所以扩容之后要更新
void reserve(size_t n)
{
if(n > capacity())
{
size_t oldsize = size();
T* tmp = new T[n];
for (size_t i = 0; i < oldsize; i++)
{
tmp[i] = _begin[i];
}
delete[] _begin;
_begin = tmp;
_finish = tmp +oldsize;
_end = tmp + n;
}
}
void resize(size_t n, const T& value = T())
{
if (n < size())
{
_finish = _begin + n;
}
else
{
if (n > capacity())
{
reserve(n);
}
while(_finish < _begin + n)
{
*(_finish++) = value;
}
}
}
修改vector(头插,尾插,插入,删除,交换)
void push_back(const T& x)
void pop_back()
iterator insert(iterator pos, const T& x)
iterator erase(iterator pos)
void swap(vector& v)
void push_back(const T& x)
{
if (_finish == _end)
{
reserve(capacity() == 0? 4: capacity()*2);
}
*_finish = x;
_finish++;
}
void pop_back()
{
assert(_begin != _finish);
_finish--;
}
//这里会发生迭代器失效问题、
//原因1:如果发生扩容函数里原pos会变成野指针需要及时更新,函数外pos不会改变,所以要返回pos的新指针
//原因2.因为插入数据,原本pos意义改变,所以要及时更新,让pos指向新插入的元素
//正确做法是返回迭代器插入的指针
iterator insert(iterator pos, const T& x)
{
assert(pos >= _begin && pos <= _finish);
if (_finish == _end)
{
size_t size = pos - _begin;
reserve(capacity() == 0 ? 4 : capacity() * 2);
pos = _begin + size;
}
iterator end = _finish - 1;
while (end >= pos)
{
*(end +1) = *(end);
end--;
}
*pos = x;
_finish++;
return pos;
}
//迭代器失效原因1.erase删除pos位置元素后,pos位置之后的元素会往前搬移,没有导致底层空间的改变,理论上讲迭代器不应该会失效,但是:如果pos刚好是最后一个元素,删完之后pos刚好是end的位置,而end位置是没有元素的,那么pos就失效了。因此删除vector中任意位置上元素时,vs就认为该位置迭代器失效了。
//迭代器失效原因2:erase之后pos位置的意义改变,迭代器所以要更新
//正确做法是返回迭代器下一个的指针
iterator erase(iterator pos)
{
assert(pos >= _begin && pos <_finish );
iterator end = pos + 1;
while (end < _finish)
{
*(end - 1) = *(end);
end++;
}
_finish--;
return pos;
}
void swap(vector<T>& v)
{
std::swap(_begin,v._begin);
std::swap(_finish,v._finish);
std::swap(_end,v._end);
}
vector的迭代器
typedef T* iterator;
typedef const T* const_iterator;
完整代码
namespace zjy
{
template<class T>
class vector
{
public:
typedef T* iterator;
typedef const T* const_iterator;
iterator begin()
{
return _begin;
}
iterator end()
{
return _finish;
}
const_iterator cbegin()
{
return _begin();
}
const_iterator cend() const
{
return _finish;
}
vector()
:_begin(nullptr)
,_finish(nullptr)
,_end(nullptr)
{
}
vector(int n, const T& value = T())
:_begin(nullptr)
, _finish(nullptr)
, _end(nullptr)
{
_begin = new T[n];
_finish = _begin + n;
_end = _begin + n;
for (int i = 0; i < n; i++)
{
*(_begin + i) = value;
}
}
//vector(int n, const T& value = T())
//{
// reserve(n);
// for (int i = 0; i < n; i++)
// {
// _begin[i] = value;
// }
//}
//vector(int n, const T& value = T())
//{
// resize(n,value);
//}
template<class InputIterator>
vector(InputIterator first, InputIterator last)
{
while (first!= last)
{
push_back(*first);
first++;
}
}
vector(vector<T>& v)
{
size_t size = v._finish - v._begin;
size_t capacity = v. _end - v._begin;
_begin = new T[capacity];
for (size_t i = 0; i < size; i++)
{
*(_begin + i) = *(v._begin + i);
}
_finish = _begin + size;
_end = _begin + capacity;
}
//vector(vector<T>& v)
//{
// vector<T> tmp(v.begin(),v.end());
// swap(tmp);
//}
vector<T>& operator = (vector<T> v)
{
swap(v);
return *this;
}
~vector()
{
if (_begin)
{
delete[] _begin;
_begin = _end = _finish = nullptr;
}
}
size_t size() const
{
return _finish - _begin;
}
size_t capacity() const
{
return _end - _begin;
}
void reserve(size_t n)
{
if(n > capacity())
{
size_t oldsize = size();
T* tmp = new T[n];
for (size_t i = 0; i < oldsize; i++)
{
tmp[i] = _begin[i];
}
delete[] _begin;
_begin = tmp;
_finish = tmp +oldsize;
_end = tmp + n;
}
}
void resize(size_t n, const T& value = T())
{
if (n < size())
{
_finish = _begin + n;
}
else
{
if (n > capacity())
{
reserve(n);
}
while(_finish < _begin + n)
{
*(_finish++) = value;
}
}
}
T& operator[](size_t pos)
{
assert(pos >= 0 && pos < size());
return _begin[pos];
}
const T& operator[](size_t pos)const
{
assert(pos >= _begin && pos < _finish);
return _begin[pos];
}
void push_back(const T& x)
{
if (_finish == _end)
{
reserve(capacity() == 0? 4: capacity()*2);
}
*_finish = x;
_finish++;
}
void pop_back()
{
assert(_begin != _finish);
_finish--;
}
void swap(vector<T>& v)
{
std::swap(_begin,v._begin);
std::swap(_finish,v._finish);
std::swap(_end,v._end);
}
iterator insert(iterator pos, const T& x)
{
assert(pos >= _begin && pos <= _finish);
if (_finish == _end)
{
size_t size = pos - _begin;
reserve(capacity() == 0 ? 4 : capacity() * 2);
pos = _begin + size;
}
iterator end = _finish - 1;
while (end >= pos)
{
*(end +1) = *(end);
end--;
}
*pos = x;
_finish++;
return pos;
}
iterator erase(iterator pos)
{
assert(pos >= _begin && pos <_finish );
iterator end = pos + 1;
while (end < _finish)
{
*(end - 1) = *(end);
end++;
}
_finish--;
return pos;
}
private:
T* _begin = nullptr;
T* _finish = nullptr;
T* _end = nullptr;
};
}