目录
vector的介绍
vector的使用
对象的定义
遍历
reserve与resize
insert与erase
迭代器失效
vector的模拟实现
vector的介绍:
vector是表示可变大小数组的序列容器。
vector的使用:
对象的定义:
void test_vector1()
{
vector<int> v1;
vector<int> v2(10, 8); //10个8
vector<int> v3(++v2.begin(), --v2.end());
vector<int> v4(v3);
string s("hello world");
vector<char> v5(s.begin(), s.end());
}
遍历:
有三种遍历方式,分别是下标,迭代器和范围for
void test_vector2()
{
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
//遍历
for (size_t i = 0; i < v.size(); ++i)
{
v[i] += 1;
cout << v[i] << " ";
}
cout << endl;
vector<int>::iterator it = v.begin();
while (it != v.end())
{
*it -= 1;
cout << *it << " ";
++it;
}
cout << endl;
for (auto e : v)
{
cout << e << " ";
}
cout << endl;
}
reserve与resize(push_back)
[]和at对越界的处理方式不一样,下标访问采用的是断言,at使用的是抛异常。
void test_vector3()
{
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
cout << v.max_size() << endl;
v.reserve(100); //扩容
v.resize(100, 5);
v.resize(2);
/*v[3]; //断言
v.at(3);*/ //抛异常
}
insert与erase(assign find 与clear)
插入和删除要注意迭代器失效。
void test_vector4()
{
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
//v.assign(10, 5); //改成10个5
vector<int>::iterator ret = find(v.begin(), v.end(), 3);
if (ret != v.end())
{
cout << "找到了" << endl;
v.insert(ret, 30);
//v.erase(ret); //不能这样删,因为ret失效了。
}
v.insert(v.begin(), -1);
for (auto e : v)
{
cout << e << " ";
}
cout << endl;
vector<int>::iterator pos = find(v.begin(), v.end(),30);
if (pos != v.end())
{
v.erase(pos);
}
for (auto e : v)
{
cout << e << " ";
}
cout << endl;
v.clear();
for (auto e : v)
{
cout << e << " ";
}
cout << endl;
}
迭代器失效:
插入会导致vector扩容,vector底层原理旧空间被释放掉, 但是it还使用的是释放之间的旧空间,在对it迭代器操作时,实际操作的是一块已经被释放的空间,而引起代码运行时崩溃。
解决方式:在以上操作完成之后,如果想要继续通过迭代器操作vector中的元素,只需给it重新
赋值即可。
erase删除pos位置元素后,pos位置之后的元素会往前搬移,没有导致底层空间的改变,理论上讲迭代 器不应该会失效,但是:如果pos刚好是最后一个元素,删完之后pos刚好是end的位置,而end位置是 没有元素的,那么pos就失效了。因此删除vector中任意位置上元素时,vs就认为该位置迭代器失效 了。
vector的模拟实现:
#pragma once
namespace qwe
{
template<class T>
class vector
{
public:
typedef T* iterator;
typedef const T* const_iterator;
vector()
:_start(nullptr)
, _finish(nullptr)
, _endofstorage(nullptr)
{}
v2(v1)
传统写法
//vector(const vector<T>& v)
//{
// _start = new T[v.capacity()];
// _finish = _start + v.size();
// _endofstorage = _start + v.capacity();
// memcpy(_start, v._start, v.size()*sizeof(T));
//}
//一个类模板的成员函数,又可以是一个函数模板
template <class InputIterator>
vector(InputIterator first, InputIterator last)
:_start(nullptr)
, _finish(nullptr)
, _endofstorage(nullptr)
{
while (first != last)
{
push_back(*first);
++first;
}
}
void swap(vector<T>& v)
{
std::swap(_start, v._start);
std::swap(_finish, v._finish);
std::swap(_endofstorage, v._endofstorage);
}
//v2(v1)
//现代写法
//vector(const vector& v)
vector(const vector<T>& v)
:_start(nullptr)
, _finish(nullptr)
, _endofstorage(nullptr)
{
vector<T> tmp(v.begin(), v._end());
swap(tmp);
}
//v3=v1
//vector& operator=(vector v)
vector<T>& operator=(vector<T> v)
{
swap(v);
return *this;
}
~vector()
{
if (_start)
{
delete[] _start;
_start = _finish = _endofstorage = nullptr;
}
}
const_iterator begin()const
{
return _start;
}
const_iterator end()const
{
return _finish;
}
iterator begin()
{
return _start;
}
iterator end()
{
return _finish;
}
const T& operator[](size_t i)const
{
assert(i < size());
return _start[i];
}
T& operator[](size_t i)
{
assert(i < size());
return _start[i];
}
size_t size()const
{
return _finish - _start;
}
size_t capacity()const
{
return _endofstorage - _start;
}
void reserve(size_t n)
{
if (n > capacity())
{
size_t sz = size();
T* tmp = new T[n];
if (_start)
{
//memcpy(tmp, _start, sizeof(T)*size());
for (size_t i = 0; i < sz; ++i)
{
//T是int一个一个拷贝没问题
//T是自定义类型,深拷贝赋值
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
{
if (n>capacity())
{
reserve(n);
}
while (_finish != _start + n)
{
*_finish = val;
++_finish;
}
}
}
iterator insert(iterator pos, const T& x)
{
assert(pos >= _start);
assert(pos <= _finish);
//满了扩容
if (_finish == _endofstorage)
{
//扩容会导致pos失效
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;
return pos;
}
iterator erase(iterator pos)
{
assert(pos >= _start);
assert(pos <= _finish);
iterator begin = pos + 1;
while (begin < _finish)
{
*(begin - 1) = *begin;
++begin;
}
--_finish;
return pos;
}
void push_back(const T& x)
{
if (_finish == _endofstorage)
{
reserve(capacity() == 0 ? 4 : capacity() * 2);
}
*_finish = x;
++_finish;
}
void pop_back()
{
assert(_finish > _start);
--_finish;
}
private:
iterator _start;
iterator _finish;
iterator _endofstorage;
};
void test_vector1()
{
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);
for (size_t i = 0; i < v.size(); ++i)
{
cout << v[i] << " ";
}
cout << endl;
vector<int>::iterator it = v.begin();
while (it != v.end())
{
cout << *it << " ";
++it;
}
cout << endl;
for (auto e : v)
{
cout << e << " ";
}
cout << endl;
}
void test_vector2()
{
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
vector<int>::iterator it = find(v.begin(), v.end(), 2);
if (it != v.end())
{
v.insert(it,20); //it会失效
}
for (auto e : v)
{
cout << e << " ";
}
cout << endl;
}
void test_vector3()
{
//1 2 3 4 5
//1 2 3 4
//1 2 3 4 5
vector<int> v1;
v1.push_back(1);
v1.push_back(2);
v1.push_back(3);
v1.push_back(4);
v1.push_back(5);
//删掉使用的偶数
vector<int>::iterator it = v1.begin();
while (it != v1.end())
{
if (*it % 2 == 0)
{
it = v1.erase(it);
}
else
{
++it;
}
}
for (auto e : v1)
{
cout << e << " ";
}
cout << endl;
}
}