文章目录
-
- [💞1. 无参默认构造](#💞1. 无参默认构造)
- [💞2. size](#💞2. size)
- [💞3. capacity](#💞3. capacity)
- [💞4. reserve](#💞4. reserve)
- [💞5. push_back](#💞5. push_back)
- [💞6. operator[]](#💞6. operator[])
- [💞7. 析构函数](#💞7. 析构函数)
- [💞8. 迭代器](#💞8. 迭代器)
- [💞9. empty](#💞9. empty)
- [💞10. pop_back](#💞10. pop_back)
- [💞11. insert](#💞11. insert)
- [💞12.构造函数(initializer_list 版本)](#💞12.构造函数(initializer_list 版本))
- 💞13.empty
- 💞14.resize
- 💞15.拷贝构造
- 💞16.赋值重载
- 💞17.迭代器区间构造
- 💞18.n个val构造
💞1. 无参默认构造
cpp
复制代码
vector()
:_start(nullptr)
, _finish(nullptr)
, _endofstorage(nullptr)
{}
💞2. size
cpp
复制代码
size_t size() const
{
return _finish - _start;
}
💞3. capacity
cpp
复制代码
size_t capacity() const
{
return _endofstorage - _start;
}
💞4. reserve
cpp
复制代码
void reserve(size_t n)
{
if (n > capacity())
{
size_t oldSize = size();
T* tmp = new T[n];
if (_start)
{
for(size_t i = 0;i < OldSize; i++)
{
tmp[i] = _start[i];
}
delete[] _start;
}
_start = tmp;
_finish = _start + oldSize;
_endofstorage = _start + n;
}
}
💞5. push_back
cpp
复制代码
//普通版本
void push_back(const T& x)
{
if (_finish == _endofstorage)
{
reserve(capacity() == 0 ? 4 : capacity() * 2);
}
*finish = x;
++_finish;
}
//insert的复用
void push_back()
{
insert(_finish,x);
}
💞6. operator[]
cpp
复制代码
//普通版本
T& operator[](size_t i)
{
assert(i < size());
return _start[i];
}
//const版本
const T& operator[](size_t i) const
{
assert(i < size());
return _start[i];
}
💞7. 析构函数
cpp
复制代码
~vector()
{
if (_start)
{
delete[] _start;
_start = _finish = _endofstorage = nullptr;
}
}
💞8. 迭代器
cpp
复制代码
typedef T* iterator;
typedef const T* const_iterator;
//迭代器
iterator begin()
{
return _start;
}
iterator end()
{
return _finish;
}
//const迭代器
const_iterator begin() const
{
return _start;
}
const_iterator end() const
{
return _finish;
}
💞9. empty
cpp
复制代码
bool empty()
{
return _start == _finish;
}
💞10. pop_back
cpp
复制代码
void pop_back()
{
assert(!empty());
--_finish;
}
💞11. insert
cpp
复制代码
iterator insert(iterator pos, const T& x)
{
assert(pos >= _start && pos <= _finish);
if (_finish == _endofstorage)
{
size_t len = pos - _start; //不加这句会造成迭代器失效,扩容了pos还指向就空间,就成野指针了
reserve(capacity() == 0 ? 4 : capacity() * 2);
pos = _start + len;
}
iterator i = _finish - 1;
while (i >= pos)
{
*(i + 1) = *i;
--i;
}
*pos = x;
++_finish;
return pos;//返回新插入的位置
}
💞12.构造函数(initializer_list 版本)
cpp
复制代码
vector(initializer_list<T> il)
{
reserve(il.size());
for (auto& e : il)
{
push_back(e);
}
}
💞13.empty
cpp
复制代码
iterator erase(iterator pos)
{
assert(pos >= _start);
assert(pos < _finish);
iterator i = pos + 1;
while (i < _finish)
{
*(i - 1) = *i;
++i;
}
--_finish;
return pos;//返回刚刚被删除位置的下一个位置
}
💞14.resize
cpp
复制代码
void resize(size_t n, T val = T())
{
if (n < size()) //小于size相当于删除
{
_finish = _start + n;
}
else
{
reserve(n);//先扩容
while (_finish < _start + n)
{
*_finish = val;
++_finish;
}
}
}
💞15.拷贝构造
cpp
复制代码
//拷贝构造
vector(const vector<T>& v)
{
reserve(v.capacity());
for (auto& e : v)
{
push_back(e);
}
}
💞16.赋值重载
cpp
复制代码
//交换
void swap(vector<T>& tmp)
{
std::swap(_start,tmp._start);
std::swap(_finish,tmp._finish);
std::swap(_endofstorage,tmp._endofstorage);
}
//赋值重载
//v1 = v3;
vector<T>& operator=(vector<T> v)//传值传参会调用拷贝构造,v3拷贝给v
{
swap(v); //v和v1交换,函数结束调用析构函数消除v
return *this;
}
💞17.迭代器区间构造
cpp
复制代码
template<class InputIterator>
vector(InputIterator first, InputIterator last)
{
while (first != last)
{
push_back(*first);
++first;
}
}
💞18.n个val构造
cpp
复制代码
vector(int n, const T& val = T())
{
reserve(n);
for (size_t i = 0; i < n; i++)
{
push_back(val);
}
}