1.成员函数
vector()
:_start(nullptr)
,_finish(nullptr)
,_endofstorage(nullptr)
{}
//v2(v1)
vector(const vector<T>& v)
:_start(nullptr)
, _finish(nullptr)
, _endofstorage(nullptr) //不初始化reserve都是随机值
{
reserve(v.capacity());//先让v2 reserve,再把v1的数据放到v2
for (auto& e : v)//加上引用是因为万一T是大对象
{
push_back(e);
}
}
void swap(vector<T>& v)
{
std::swap(_start, v._start); //库里面
std::swap(_finish, v._finish);
std::swap(_endofstorage, v._endofstorage);
}
//v3=v1 tmp就是v1的深拷贝
vector<T>& operator=(vector<T> tmp)//用现代写法,传参就得调用拷贝构造
{
swap(tmp);
return *this;
}
~vector()
{
delete[] _start;
_start = _finish = _endofstorage = nullptr;
}
2.迭代器
public:
typedef T* iterator; //typedef是受访问限定符的限定,想给别人用就放到公有
typedef const T* const iterator;//类比于const T*,是指向的内容不能修改
iterator begin()
{
return _start;
}
iterator end()
{
return _finish;
}
const_iterator begin() const
{
return _start;
}
const_iterator end() const
{
return _finish;
}
3.空间
扩容:
resize:
size_t capacity()
{
return _endofstorage - _start;
}
size_t size()
{
return _finish - _start;
}
void resize(size_t n, const T& val = T()) //缺省值不能直接给0,因为不能确定T就是int T可能是
{ string vector<int>,所以给了T类型的匿名对象
if (n <= size())
{
_finish = _start + n;
}
else
{
reserve(n);//让reserve去检查>
while (_finish < _start + n)
{
*_finish = val;
++_finish;
}
}
}
void reserve(size_t n)//扩容
{
if (n > capacity()) //n>capacity才扩容
{
size_t sz = size();
T* tmp = new T[n];
if (_start)
{
memcpy(tmp, _start, sizeof(T) * sz);
delete[] _start;
}
_start = tmp;
_finish = tmp + sz;
_endofstorage = _start + n;
}
}
4.访问操作
T& operator[](size_t pos)
{
assert(pos < size());
return _start[pos];
}
const T& operator[](size_t pos) const //只能读不能写
{
assert(pos < size());
return _start[pos];
}
5.增删查改
void push_back(const T& x)
{
if (_finish == _endofstorage)
{
size_t sz = size();
size_t cp = capacity() == 0 ? 4 : capacity() * 2;
T* tmp = new T[cp];
if (_start)
{
memcpy(tmp, _start, sizeof(T) * sz);
delete[] _start;
}
_start = tmp;
_finish = tmp + sz;
_endofstorage = _start + cp;
}
*_finish = x;
++_finish;
}
void erase(iterator pos)
{
assert(pos >= _start);
assert(pos < _finish);
iterator it = pos + 1;
while (it <_finish)
{
*(it - 1) = *it;
++it;
}
--_finish;
}
void insert(iterator pos, const T& x)
{
assert(pos >= _start);
assert(pos <= _finish);
if (_finish == _endofstorage)
{
size_t len = pos - _start;
reserve(capacity() == 0 ? 4 : capacity() * 2);
pos = _start + len;
}
iterator end = _finish -1;
while (end >= pos)
{
*(end + 1) = *end;
end--;
}
*pos = x;
++_finish;
}
删:
insert:
6.迭代器失效
6.1扩容导致的内部失效
void insert(iterator pos, const T& x)
{
assert(pos >= _start);
assert(pos <= _finish);
if (_finish == _endofstorage)
{
size_t len = pos - _start;
reserve(capacity() == 0 ? 4 : capacity() * 2);
pos = _start + len;
}
iterator end = _finish -1;
while (end >= pos)
{
*(end + 1) = *end;
end--;
}
*pos = x;
++_finish;
}
void test()
{
vector<int>::iterator it = v.begin();insert插入后it就失效了,不能访问
while (it != v.end())
{
cout << *it << " ";
++it;
}
}
cout << endl;
pos还指向之前旧的空间,扩容后pos失效了,需要更新pos
6.2 erase中的迭代器失效
pos是否会失效?迭代器失效:如果失效就不能在使用这个迭代器,如果使用了,结果是未定义的。
void erase(iterator pos)
{
assert(pos >= _start);
assert(pos < _finish);
iterator it = pos + 1;
while (it <_finish)
{
*(it - 1) = *it;
++it;
}
--_finish;
}
auto pos=v.begin();
v.erase(pos);
一般情况下不会失效,但是下面这种情况会失效
void test_vector4()
{
//删除所有的偶数
//1 2 3 4 5不会有错
/*vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
v.push_back(5);*/
//1 2 3 4 5 6 崩溃
//2 3 4 5 偶数没有删完,结果不对
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
v.push_back(5);
v.push_back(6);
auto it = v.begin();
while (it != v.end())
{
if (*it % 2 == 0)
{
v.erase(it);
}
++it;
}
for (auto e : v)
{
cout << e << " ";
}
cout << endl;
}
vs2019进行强制检查,erase以后认为it失效了,不能访问,访问就报错。
怎么解决?
返回的是删除数据的下一个位置
iterator erase(iterator pos)
{
assert(pos >= _start);
assert(pos < _finish);
iterator it = pos + 1;
while (it <_finish)
{
*(it - 1) = *it;
++it;
}
--_finish;
return pos;//返回当前数据的下一个位置
}
auto it = v.begin();
while (it != v.end())
{
if (*it % 2 == 0)
{
it=v.erase(it);//删除的时候接受返回值就可以了
}
else
{
++it; //不删除的时候才++
}
}
for (auto e : v)
{
cout << e << " ";
}
cout << endl;
结论:insert和erase以后迭代器都失效,不能再访问。
7.深拷贝问题
void test_vector5()
{
vector<string> v;
v.push_back("11111111111111111111111");
v.push_back("11111111111111111111111");
v.push_back("11111111111111111111111");
v.push_back("11111111111111111111111");
v.push_back("11111111111111111111111");
for (auto e : v)
{
cout << e << " ";
}
cout << endl;
}
报错!是因为扩容中memcpy导致的问题
应该是
void reserve(size_t n)//扩容
{
if (n > capacity()) //n>capacity才扩容
{
size_t sz = size();
T* tmp = new T[n];
if (_start)
{
//memcpy(tmp, _start, sizeof(T) * sz);
for (size_t i = 0; i < sz; i++)
{
tmp[i] = _start[i]; // string的赋值就是深拷贝
}
delete[] _start;
}
_start = tmp;
_finish = tmp + sz;
_endofstorage = _start + n;
}
}
源码:
#pragma once
#include<stdlib.h>
#include<assert.h>
#include<iostream>
using namespace std;
namespace bit
{
template<class T>
class vector
{
public:
typedef T* iterator; //typedef是受访问限定符的限定,想给别人用就放到公有
typedef const T* const_iterator;//类比于const T*,是指向的内容不能修改
iterator begin()
{
return _start;
}
iterator end()
{
return _finish;
}
const_iterator begin() const
{
return _start;
}
const_iterator end() const
{
return _finish;
}
vector()
:_start(nullptr)
,_finish(nullptr)
,_endofstorage(nullptr)
{}
//v2(v1)
vector(const vector<T>& v)
:_start(nullptr)
, _finish(nullptr)
, _endofstorage(nullptr) //不初始化reserve都是随机值
{
reserve(v.capacity());//先让v2 reserve,再把v1的数据放到v2
for (auto& e : v)//加上引用是因为万一T是大对象
{
push_back(e);
}
}
void swap(vector<T>& v)
{
std::swap(_start, v._start); //库里面
std::swap(_finish, v._finish);
std::swap(_endofstorage, v._endofstorage);
}
//v3=v1 tmp就是v1的深拷贝
vector<T>& operator=(vector<T> tmp)//用现代写法
{
swap(tmp);
return *this;
}
~vector()
{
delete[] _start;
_start = _finish = _endofstorage = nullptr;
}
void reserve(size_t n)//扩容
{
if (n > capacity()) //n>capacity才扩容
{
size_t sz = size();
T* tmp = new T[n];
if (_start)
{
//memcpy(tmp, _start, sizeof(T) * sz);内置类型用memcpy可以
for (size_t i = 0; i < sz; i++)
{
tmp[i] = _start[i]; //内置类型直接赋值, string的赋值就是深拷贝,自定义类型调用赋值重载,赋值重载就是深拷贝
}
delete[] _start;
}
_start = tmp;
_finish = tmp + sz;
_endofstorage = _start + n;
}
}
//void resize(size_t n,T val=T())
void resize(size_t n, const T& val = T()) //缺省值不能直接给0,因为不能确定T就是int T可能是string vector<int>,所以给了T类型的匿名对象
{
if (n <= size())
{
_finish = _start + n;
}
else
{
reserve(n);//让reserve去检查>
while (_finish < _start + n)
{
*_finish = val;
++_finish;
}
}
}
void push_back(const T& x)
{
/*if (_finish == _endofstorage)
{
reserve(capacity() == 0 ? 4 : capacity() * 2);
}
*_finish = x;
++_finish;*/
insert(end(), x);
}
void insert(iterator pos, const T& x)
{
assert(pos >= _start);
assert(pos <= _finish);
if (_finish == _endofstorage)
{
size_t len = pos - _start;
reserve(capacity() == 0 ? 4 : capacity() * 2);
pos = _start + len;
}
iterator end = _finish -1;
while (end >= pos)
{
*(end + 1) = *end;
end--;
}
*pos = x;
++_finish;
}
iterator erase(iterator pos)
{
assert(pos >= _start);
assert(pos < _finish);
iterator it = pos + 1;
while (it <_finish)
{
*(it - 1) = *it;
++it;
}
--_finish;
return pos;//返回当前数据的下一个位置
}
T& operator[](size_t pos)
{
assert(pos < size());
return _start[pos];
}
const T& operator[](size_t pos) const //只能读不能写
{
assert(pos < size());
return _start[pos];
}
size_t capacity() const
{
return _endofstorage - _start;
}
size_t size() const
{
return _finish - _start;
}
private:
iterator _start;
iterator _finish;
iterator _endofstorage;
};
void test_vector5()
{
vector<string> v;
v.push_back("11111111111111111111111");
v.push_back("11111111111111111111111");
v.push_back("11111111111111111111111");
v.push_back("11111111111111111111111");
v.push_back("11111111111111111111111");
for (auto e : v)
{
cout << e << " ";
}
cout << endl;
}
}