vector模拟实现
- 🏞️1. vector的扩容机制
- 🌁2. vector迭代器失效问题
- 📖2.1 insert导致的失效
- 📖2.2 erase导致的失效
- 🌿3. vector拷贝问题
- 🏜️4. 模拟实现vector
🏞️1. vector的扩容机制
#include<iostream>
#include<vector>
using namespace std;
int main()
{
size_t sz;
vector<int> foo;
sz = foo.capacity();
cout << "making foo grow: \n" << endl;
for (int i = 0; i < 100; ++i)
{
foo.push_back(i);
//记录每次的扩容
if (sz != foo.capacity())
{
sz = foo.capacity();
cout << "capacity changed:" << sz << endl;
}
}
return 0;
}
这是在VS版本下的扩容机制:每次扩容1.5倍
但在Linux下,它每次以2倍的方式扩容:
capacity changed: 1
capacity changed: 2
capacity changed: 4
capacity changed: 8
capacity changed: 16
capacity changed: 32
capacity changed: 64
capacity changed: 128
所以,它并不是每次必须以固定的方式扩容,而是选择一个合适的数值:
因为单次增容越多,插入N个值,当需要增容时,增容次数越少,但单次增容越多,可能浪费的空间就越多.
单次增少了,会导致频繁扩容,效率降低.
🌁2. vector迭代器失效问题
📖2.1 insert导致的失效
📖2.2 erase导致的失效
🌿3. vector拷贝问题
void reserve(size_t n)
{
//记录与_start的相对位置, 用于更新_finish
size_t sz = size();
if (n > capacity())
{
T* tmp = new T[n];
if (_start)
{
//这里使用了memcpy
memcpy(tmp, _start, size() * sizeof(T));
delete[] _start;
}
_start = tmp;
_finish = _start + sz;
_endofstorage = _start + n;
}
}
我们在对vector
进行扩容时,需要将原来空间上的数据拷贝到新开的空间上,那么拷贝数据使用memcpy
会不会有问题呢?
首先来看一个简单的vector
:
可以看到,对于上述简单的vector,使用memcpy
并不会带来什么问题,但是让我们看看下面这个场景:
所以,在扩容时,我们不能使用简单的memcpy
完成:
void reserve(size_t n)
{
//记录与_start的相对位置, 用于更新_finish
size_t sz = size();
if (n > capacity())
{
T* tmp = new T[n];
if (_start)
{
//memcpy(tmp, _start, size() * sizeof(T));
for (size_t i = 0; i < size(); ++i)
{
tmp[i] = _start[i];
}
delete[] _start;
}
_start = tmp;
_finish = _start + sz;
_endofstorage = _start + n;
}
}
🏜️4. 模拟实现vector
#pragma once
#include<cassert>
namespace stl
{
template<class T>
class vector
{
public:
typedef T* iterator;
typedef const T* const_iterator;
vector()
: _start(nullptr)
, _finish(nullptr)
, _endofstorage(nullptr)
{}
~vector()
{
if (_start)
{
delete[] _start;
_start = _finish = _endofstorage = nullptr;
}
}
template<class InputIterator>
vector(InputIterator first, InputIterator end)
: _start(nullptr)
, _finish(nullptr)
, _endofstorage(nullptr)
{
while (first != end)
{
//复用push_back
push_back(*first);
++first;
}
}
vector(size_t n, const T& val = T())
: _start(nullptr)
, _finish(nullptr)
, _endofstorage(nullptr)
{
reserve(n);
for (size_t i = 0; i < n; ++i)
{
push_back(val);
}
}
void swap(vector<T>& v)
{
std::swap(_start, v._start);
std::swap(_finish, v._finish);
std::swap(_endofstorage, v._endofstorage);
}
//vector(const vector<T>& v)
// : _start(nullptr)
// , _finish(nullptr)
// , _endofstorage(nullptr)
//{
// for (int i = 0; i < v.size(); ++i)
// {
// //拷贝内容
// push_back(v[i]);
// }
//}
//拷贝构造
vector(const vector<T>& v)
: _start(nullptr)
, _finish(nullptr)
, _endofstorage(nullptr)
{
vector<T> tmp(v.begin(), v.end());
//将局部对象的资源获取过来, 出作用域局部对象析构
swap(tmp);
}
vector<T>& operator=(const vector<T>& v)
{
if (this != &v)
{
//将自己的资源释放掉
if (_start)
{
delete[] _start;
_start = _finish = _endofstorage = nullptr;
}
reserve(v.capacity());
for (auto e : v)
{
*_finish = e;
_finish++;
}
/*for (int i = 0; i < v.size(); ++i)
{
_start[i] = v[i];
}*/
//_finish += v.size();
}
return *this;
}
//vector<T>& operator=(vector<T> v)
//{
// //将自己的资源交给局部对象释放掉, 自己拿到局部对象的资源
// swap(v);
// return *this;
//}
iterator begin()
{
return _start;
}
iterator end()
{
return _finish;
}
const_iterator begin() const
{
return _start;
}
const_iterator end() const
{
return _finish;
}
size_t size() const
{
return _finish - _start;
}
size_t capacity() const
{
return _endofstorage - _start;
}
void reserve(size_t n)
{
//记录与_start的相对位置, 用于更新_finish
size_t sz = size();
if (n > capacity())
{
T* tmp = new T[n];
if (_start)
{
//memcpy(tmp, _start, size() * sizeof(T));
for (size_t i = 0; i < size(); ++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 > capacity())
{
reserve(n);
}
if (n > size())
{
//如果有效元素变多,填充即可
while (_finish < _start + n)
{
*_finish = val;
++_finish;
}
}
else
{
//如果有效元素变少, 减少finish
_finish = _start + n;
}
}
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()
{
if (_finish > _start)
{
--_finish;
}
}
iterator insert(iterator pos, const T& val)
{
assert(pos >= _start && pos <= _finish);
//检查扩容
if (_finish == _endofstorage)
{
//记录相对位置
size_t n = pos - _start;
size_t newCapacity = capacity() == 0 ? 4 : capacity() * 2;
reserve(newCapacity);
//更新pos
pos = _start + n;
}
//挪动数据
iterator end = _finish;
while (end >= pos)
{
*(end + 1) = *end;
end++;
}
*pos = val;
++_finish;
return pos;
}
iterator erase(iterator pos)
{
assert(pos >= _start && pos <= _finish);
iterator it = pos + 1;
while (it != _finish)
{
//删除数据,覆盖,向前挪动数据
*(it - 1) = *it;
++it;
}
--_finish;
return pos;
}
void clear()
{
_finish = _start;
}
const T& operator[](size_t pos) const
{
assert(pos < size());
return _start[pos];
}
T& operator[](size_t pos)
{
assert(pos < size());
return _start[pos];
}
private:
iterator _start;
iterator _finish;
iterator _endofstorage;
};
}