vector的文档:vector - C++ Reference (cplusplus.com)
vector是数组。vector空间连续,可变大小,可存放自定义类型。
vector简单的使用
vector v; //int型实例化
v.push_back(1);//插入数据
v.push_back(2);
v.push_back(3);
for (auto x : v)//访问vector元素
{
cout << x << endl;
}
与vector相比string可以兼容c语言,有专用函数
说明vector的迭代器底层就是指针,和string一样。
namespace myvector
{
template
class vector
{
typedef T* iterator;
public:
//无参构造
vector()
:_start(nullptr)
,_finish(nullptr)
,_endofstorage(nullptr)
{
}
vector(size_t n, const T& val = T())
:_start(nullptr)
, _finish(nullptr)
, _endofstorage(nullptr)
{
resize(n, val);
}
template
vector(InputIterator first, InputIterator last)
{
while (first != last)
{
push_back(*first);
++first;
}
}
// 析构
~vector()
{
if (_start)
{
delete[] _start;
}
_start = _finish = _endofstorage = nullptr;
}
private:
iterator _start;
iterator _finish;
iterator _endofstorage;
};
}
vector的底层有_start,_finish,_endofstorage三个迭代器来实现size()表示存储数据个数,capacity()表示存储空间。
vector支持不同类型的迭代器因此在模板里面又用了模板InputIterator。
注意vector::v(10,1)会报错,而vector::v(10u,1)没问题,根本原因是10,1都是int型类型相同,会调用迭代器初始化,而10u是size_t型,会适配 vector(size_t n, const T& val = T())。
vector(const vector& v)
:_start(nullptr)
, _finish(nullptr)
, _endofstorage(nullptr)
{
reserve(v.capacity());
for (auto e : v)
{
push_back(e);
}
}
如图:会直接将string对象拷贝过去导致两个vector指向同一个空间。
解决方法:
void reserve(size_t n)
{
size_t sz = size();
size_t ca = capacity();
if (ca < n)
{
iterator tmp = new T[n];
if (_start)
{
//memcpy(tmp, _start, sizeof(T) * sz);
for (size_t i = 0; i < v.size(); i++)
{
tmp[i] = _start[i];
}
delete[] _start;
}
_start = tmp;
_finish = _start + sz;
_endofstorage = _start + n;
}
}
通过赋值重载会实现深拷贝,来实现效果。
void swap(vector& v)
{
std::swap(_start, v._start);
std::swap(_finish, v._finish);
std::swap(_endofstorage, v._endofstorage);
}
vector& operator=(vector v)
{
swap(v);
return *this;
}
这是一种巧妙的方式,注意=的参数是传值,会调用拷贝构造,这个拷贝构造的v出了作用域会自动析构,不用手动释放。
typedef T* iterator;
typedef const T* const_iterator;
iterator begin()
{
return _start;
}
const_iterator begin()const
{
return _start;
}
const_iterator end()const
{
return _finish;
}
const T& operator[](size_t pos)const
{
return _start[pos];
}
iterator end()
{
return _finish;
}
const_iterator可以应用到下面的场景
void print(const vectorv)
{
for (auto x : v)
{
cout << x;
}
}
size_t size()
{
return _finish - _start;
}
size_t capacity()
{
return _endofstorage - _start;
}
void reserve(size_t n)
{
size_t sz = size();
size_t ca = capacity();
if (ca < n)
{
iterator tmp = new T[n];
if (_start)
{
for (size_t i = 0; i < this->size(); i++)
{
tmp[i] = _start[i];
}
delete[] _start;
}
_start = tmp;
_finish = _start + sz;
_endofstorage = _start + n;
}
}
原因:reserve只是开空间,capacity=10,而size没有改变,v[i]会把i和size()进行比较。
迭代器失效:当vector扩容时,调用的insert/erase函数的迭代器可能失效。
如图当在pos处insert一个数值时,空间异地扩容,_start迭代器指向一个新的空间,pos迭代器可能还指向旧空间,变成野指针,同理erase如果导致异地缩容,pos指针也可能变成野指针。
insert的错误写法:
void insert(iterator pos, const T& x)
{
assert(pos >= _start && pos <= _finish);
if (_finish == _endofstorage)
{
size_t newcapacity = capacity() == 0 ? 4 : capacity() * 2;
reserve(newcapacity);
}
iterator end = _finish - 1;
while (end >= pos)
{
*(end + 1) = *end;
--end;
}
*pos = x;
++_finish;
}
正确写法:
iterator insert(iterator pos, const T& x)
{
assert(pos >= _start && pos <= _finish);
if (_finish == _endofstorage)
{
// 保存相对位置
size_t len = pos - _start;
size_t newcapacity = capacity() == 0 ? 4 : capacity() * 2;
reserve(newcapacity);
//更新pos
pos = _start + len;
}
iterator end = _finish - 1;
while (end >= pos)
{
*(end + 1) = *end;
--end;
}
*pos = x;
++_finish;
return pos;
}
用len保存了相对位置,在扩容后更新pos的位置,函数的实参的pos也会失效,所以返回一个指向新位置的pos来更新。
myvector::vector v;
v.insert(v.begin()+1,1);//pos可能失效
原因:因为begin()的返回值是常值,不是引用,那么常引用也不行因为pos要更新,如果begin()函数也不能返回引用,因为_start是private中的变量,不应该能被修改。begin()迭代器指向的值应该可以被修改,所以也不能begin()也不能返回常引用。
iterator erase(iterator pos)
{
assert(pos >= _start && pos < _finish);
iterator it = pos + 1;
while (it != _finish)
{
*(it - 1) = *it;
++it;
}
--_finish;
return pos;
}
resize实现
void resize(size_t n, const T& val = T())
{
if (n < size())
{
_finish = _start + n;
}
else
{
reserve(n);
while (_finish != _start + n)
{
*_finish = val;
++_finish;
}
}
}
注意第二个参数缺省值是调用构造函数,那么这其实说明C++11中内置类型数据(int,double)升级成了对象。(怎么有python那味?)
完整代码
#include
#include
namespace myvector
{
template
class vector
{
typedef T* iterator;
typedef const T* const_iterator;
public:
//无参构造
vector()
:_start(nullptr)
,_finish(nullptr)
,_endofstorage(nullptr)
{
}
vector(size_t n, const T& val = T())
:_start(nullptr)
, _finish(nullptr)
, _endofstorage(nullptr)
{
resize(n, val);
}
template
vector(InputIterator first, InputIterator last)
{
while (first != last)
{
push_back(*first);
++first;
}
}
vector(const vector& v)
:_start(nullptr)
, _finish(nullptr)
, _endofstorage(nullptr)
{
/*_start = new T[v.capacity()];
memcpy(_start, v._start,sizeof(T)*v.size());
_finish = _start + v.size();
_endofstorage = _start + v.capacity();*/
reserve(v.capacity());
for (auto e : v)
{
push_back(e);
}
}
iterator begin()
{
return _start;
}
const_iterator begin()const
{
return _start;
}
const_iterator end()const
{
return _finish;
}
const T& operator[](size_t pos)const
{
return _start[pos];
}
iterator end()
{
return _finish;
}
size_t size()
{
return _finish - _start;
}
size_t capacity()
{
return _endofstorage - _start;
}
void swap(vector& v)
{
std::swap(_start, v._start);
std::swap(_finish, v._finish);
std::swap(_endofstorage, v._endofstorage);
}
vector& operator=(vector v)
{
swap(v);
return *this;
}
T& operator[](size_t n)
{
return *(_start + n);
}
void reserve(size_t n)
{
size_t sz = size();
size_t ca = capacity();
if (ca < n)
{
iterator tmp = new T[n];
if (_start)
{
//memcpy(tmp, _start, sizeof(T) * sz);
for (size_t i = 0; i < this->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 < size())
{
_finish = _start + n;
}
else
{
reserve(n);
while (_finish != _start + n)
{
*_finish = val;
++_finish;
}
}
}
iterator insert(iterator pos, const T& x)
{
assert(pos >= _start && pos <= _finish);
if (_finish == _endofstorage)
{
size_t len = pos - _start;
size_t newcapacity = capacity() == 0 ? 4 : capacity() * 2;
reserve(newcapacity);
pos = _start + len;
}
iterator end = _finish - 1;
while (end >= pos)
{
*(end + 1) = *end;
--end;
}
*pos = x;
++_finish;
return pos;
}
void push_back(const T& val)
{
/*if (_endofstorage == _finish)
{
reserve(capacity() == 0 ? 4 : 2 * capacity());
}
*_finish++ = val;
*/
insert(_finish, val);
}
iterator erase(iterator pos)
{
assert(pos >= _start && pos < _finish);
iterator it = pos + 1;
while (it != _finish)
{
*(it - 1) = *it;
++it;
}
--_finish;
return pos;
}
private:
iterator _start;
iterator _finish;
iterator _endofstorage;
};
}