本质讲,vector使用动态分配数组来存储它的元素
string、vector等都有一个特点,删除数据,一般是不会主动缩容
构造函数声明
|
接口说明
|
vector()
|
无参构造
|
vector(size_type n, const value_type& val = value_type())
|
构造并初始化n个val
|
vector (const vector& x);
|
拷贝构造
|
vector (InputIterator fifirst, InputIterator last);
|
使用迭代器进行初始化构造
|
- int main()
- {
- //v1 12345
- vector<int> v1;
- v1.push_back(1);
- v1.push_back(2);
- v1.push_back(3);
- v1.push_back(4);
- v1.push_back(5);
-
- //v2 1.1,2.2,3.3,4.4,5.5
- vector<double> v2;
- v2.push_back(1.1);
- v2.push_back(2.2);
- v2.push_back(3.3);
- v2.push_back(4.4);
- v2.push_back(5.5);
-
- //v3 克里斯蒂亚诺里昂内尔内马尔
- vector
v3; - v3.push_back("克里斯蒂亚诺");
- v3.push_back("里昂内尔");
- v3.push_back("内马尔");
-
-
- //v4构造并初始化10个5
- vector<int> v4(10, 5);
-
- //v5 里斯蒂亚诺里昂内尔内马
- vector
v5(++v3.begin(), --v3.end()) ; -
- string s = "hello world";
-
- //v6 "hello world"
- vector<char> v6(s.begin(), s.end());
-
- return 0;
- }
iterator的使用
|
接口说明
|
begin + end
|
获取第一个数据位置的iterator/const_iterator, 获取最后一个数据的下一个位置的iterator/const_iterator
|
rbegin + rend
|
获取最后一个数据位置的reverse_iterator,获取第一个数据前一个位置的
reverse_iterator
|
- int main()
- {
- vector<int> v1;
- v1.push_back(1);
- v1.push_back(2);
- v1.push_back(3);
- v1.push_back(4);
- v1.push_back(5);
-
- //遍历
- //1.下标+[]
- for (size_t i = 0; i < v1.size(); i++)
- {
- cout << v1[i] << " ";
- }
- cout << endl;
-
- //2.迭代器
- vector<int> ::iterator it = v1.begin();
- while (it != v1.end())
- {
- cout << *it << " ";
- it++;
- }
- cout << endl;
-
- //3.范围for
- for (auto c : v1)
- {
- cout << c << " ";
- }
- cout << endl;
-
- return 0;
- }
vector迭代器失效有两种:
- 扩容、缩容,导致野指针式失效。
- 迭代器指向的意义变了
vector检查迭代器失效的两种机制:
- 系统越界机制检查(不一定能检查到)
- 编译实现机制检查(相对靠谱)
对于insert和erase造成的迭代器失效问题,linux(g++)平台检查很佛系,基本依靠操作系统野指针越界检查机制;windows下VS系列检查更严格,使用一些强制检查机制,意义变了也可能会检查出来。
vector构建动态二维数组时与上图实际是一致的
- namespace Cris
- {
- template <class T>
- class vector
- {
- public:
- typedef T* iterator;
- typedef const T* const_iterator;
-
- vector()
- :_start(nullptr)
- , _finish(nullptr)
- , _endofstoage(nullptr)
- {}
-
- template <class InputIterator>
- vector(InputIterator first, InputIterator last)
- : _start(nullptr)
- , _finish(nullptr)
- , _endofstoage(nullptr)
- {
- while (first != last)
- {
- push_back(*first);
- ++first;
- }
- }
-
- vector(size_t n, const T& val = T())
- : _start(nullptr)
- , _finish(nullptr)
- , _endofstoage(nullptr)
- {
- reserve(n);
- for (size_t i = 0; i < n; ++i)
- {
- push_back(val);
- }
- }
-
- vector(int n, const T& val = T())
- : _start(nullptr)
- , _finish(nullptr)
- , _endofstoage(nullptr)
- {
- reserve(n);
- for (int i = 0; i < n; ++i)
- {
- push_back(val);
- }
- }
-
- void swap(vector
& v) - {
- std::swap(_start, v._start);
- std::swap(_finish, v._finish);
- std::swap(_endofstoage, v._endofstoage);
- }
-
- vector(const vector
& v) - : _start(nullptr)
- , _finish(nullptr)
- , _endofstoage(nullptr)
- {
- vector
tmp(v.begin(), v.end()) ; - swap(tmp);
- }
-
- vector
& operator=(vector v) - {
- swap(v);
- return *this;
- }
-
-
-
- iterator begin()
- {
- return _start;
- }
-
- iterator end()
- {
- return _finish;
- }
-
- const_iterator begin() const
- {
- return _start;
- }
-
- const_iterator end() const
- {
- return _finish;
- }
-
- size_t size() const
- {
- return _finish - _start;
- }
-
- size_t capacity() const
- {
- return _endofstoage - _start;
- }
-
- void reserve(size_t n)
- {
- size_t sz = size();
- if (n > capacity)
- {
- T* tmp = new T[n];
- if (_start)
- {
- //扩容使用memcpy拷贝数据是存在浅拷贝问题
- //memcpy(tmp, _start, size() * sizeof(T));
- //解决方法:
- for (size_t i = 0; i < size(); i++)
- {
- tmp[i] = _start[i];
- }
- delete[] _start;
- }
-
- _start = tmp;
- }
-
- _finish = _start + sz;
- _endofstoage = _start + n;
- }
-
- //T() ---- T的构造函数
- //void resize(size_t n,const T& val = T())
- void resize(size_t n, T val = T())
- {
- if (n > capacity())
- {
- reserve(n);
- }
-
- if (n > size())
- {
- while (_finish < _start + n)
- {
- *_finish = val;
- ++_finish;
- }
- }
- else
- {
- _finish = _start + n;
- }
- }
-
- void push_back(const T& x)
- {
- /*if (_finish == _endofstoage)
- {
- size_t newCapacity = capacity() == 0 ? 4 : capacity() * 2;
- reserve(newCapacity);
- }
- *_finish = x;
- ++_finish;*/
-
- insert(end(), x);
- }
-
- void pop_back()
- {
- if (_finish < _start)
- {
- --_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];
- }
-
- iterator insert(iterator pos, const T& x)
- {
- // 检查参数
- assert(pos >= _start && pos <= _finish);
-
- // 扩容
- // 扩容以后pos就失效了,需要更新一下
- if (_finish == _endofstoage)
- {
- size_t n = pos - _start;
-
- size_t newCapacity = capacity() == 0 ? 4 : capacity() * 2;
- reserve(newCapacity);
-
- pos = _start + n;
- }
-
- // 挪动数据
- iterator end = _finish - 1;
- while (end >= pos)
- {
- *(end + 1) = *end;
- --end;
- }
-
- *pos = x;
- ++_finish;
-
- return pos;
- }
-
- iterator erase(iterator pos)
- {
- assert(pos >= _start && pos < _finish);
- iterator it = pos + 1;
- while (it != _finish)
- {
- *(it - 1) = *it;
- ++it;
- }
-
- --_finish;
-
- return pos;
- }
-
- void clear()
- {
- _finish = _start;
- }
-
- private:
- iterator _start;
- iterator _finish;
- iterator _endofstoage;
- };
- }