一、简述
vector是表示可变大小数组的序列容器。
二、stl-vector 模拟实现
vector.h
cpp
template<class T>
class vector
{
public:
typedef T* iterator;
typedef const T* const_iterator;
//根据正向迭代器来实现反向迭代器
typedef ReverseIterator<iterator, T&, T*> reverse_iterator;
//也可以在参数列表里初始化
//构造初始化
vector() :_start(nullptr), _finish(nullptr), _end_of_storage(nullptr) {}
// 匿名对象生命周期只在当前一行,而const引用会将匿名对象生命周期到引用对象的域结束
vector(size_t n, const T& val = T())
: _start(nullptr)
, _finish(nullptr)
, _end_of_storage(nullptr)
{
reserve(n);
for (size_t i = 0; i < n; ++i)
{
push_back(val);
}
}
template <class InputIterator>
vector(InputIterator first, InputIterator last)
{
while (first != last)
{
push_back(*first);
++first;
}
}
//深拷贝
vector(const vector<T>& v)
{
//复用
/*reserve(v.capacity());
for (auto e : v)
{
push_back(e);
}*/
//传统写法
reserve(v.capacity());
//memcpy(_start, v._start, sizeof(T) * v.size());
//memcpy具有很大的隐患
//对于内置类型无影响
//而对于复合类型则是浅拷贝
for (size_t i = 0; i < v.size(); ++i)
{
_start[i] = v._start[i];
}
_finish = _start + v.size();
}
//析构
~vector()
{
delete[] _start;
_start = _finish = _end_of_storage = nullptr;
}
iterator begin() { return _start; }
iterator end() { return _finish; }
reverse_iterator rbegin() { return end(); }
reverse_iterator rend() { return begin(); }
const_iterator begin() const { return _start; }
const_iterator end() const { return _finish; }
//容量
size_t capacity() const { return _end_of_storage - _start; }
//数量
size_t size() const { return _finish - _start; }
//判空
bool empty() { return _start == _finish; }
//扩容
void reserve(size_t n)
{
if (n > capacity())
{
size_t sz = size();
T* tmp = new T[n];
if (_start)
{
//memcpy浅拷贝
//memcpy(tmp, _start, sizeof(T) * size());
for (size_t i = 0; i < sz; ++i)
{
tmp[i] = _start[i];
}
delete[] _start;
}
_start = tmp;
_finish = tmp + sz;//因为_start在上方被修改,所以用提前记录size的sz变量
_end_of_storage = tmp + n;
}
}
//扩容加初始化
void resize(size_t n, T val = T())
{
if (n < size())
_finish = _start + n;
else
{
if (n > capacity())
reserve(n);
while (_finish != _start + n)
{
*_finish = val;
++_finish;
}
}
}
//尾插
void push_back(const T& x)
{
if (_finish == _end_of_storage)
{
reserve(capacity() == 0 ? 4 : capacity() * 2);
}
*_finish = x;
++_finish;
}
//尾删
void pop_back()
{
assert(!empty());
--_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];
}
//重载=
vector<T>& operator=(const vector<T>& v)
{
reserve(v.capacity());
for (size_t i = 0; i < v.size(); ++i)
{
_start[i] = v._start[i];
}
_finish = _start + v.size();
_end_of_storage = _start + v.capacity();
return *this;
}
//查找
iterator find(size_t pos)
{
assert(pos <= size());
return _start + pos;
}
//插入
iterator insert(iterator pos, const T& val)
{
assert(pos <= _finish && pos >= _start);
if (_finish == _end_of_storage)
{
size_t len = pos - _start;
reserve(capacity() == 0 ? 4 : capacity() * 2);
//扩容后更新pos的地址,否则会引发迭代器失效问题
pos = _start + len;
}
iterator end = _finish - 1;
while (end >= pos)
{
*(end + 1) = *end;
--end;
}
*pos = val;
++_finish;
return pos;
}
//删除
void earse(iterator pos)
{
assert(pos >= _start && pos < _finish);
assert(!empty());
iterator fir = pos;
while (fir != _finish - 1)
{
*fir = *(fir + 1);
++fir;
}
--_finish;
}
private:
iterator _start;
iterator _finish;
iterator _end_of_storage;
};
template<class T>
void func(const vector<T>& v)
{
for (size_t i = 0; i < v.size(); i++)
cout << v[i] << "->";
cout << endl;
//迭代器
for (auto ch : v)
cout << ch << "->";
cout << endl;
vector<int>::const_iterator it = v.begin();
while (it != v.end())
{
cout << *it << "->";
++it;
}
}
//杨辉三角
vector<vector<int>> generate(int numRows) {
vector<vector<int>> vv;
vv.resize(numRows, vector<int>());
for (size_t i = 0; i < vv.size(); ++i)
{
vv[i].resize(i + 1, 0);
vv[i][0] = vv[i][vv[i].size() - 1] = 1;
}
for (size_t i = 0; i < vv.size(); ++i)
{
for (size_t j = 0; j < vv[i].size(); ++j)
{
if (vv[i][j] == 0)
{
vv[i][j] = vv[i - 1][j - 1] + vv[i - 1][j];
}
}
}
return vv;
}