【C++】C++ STL探索:Vector使用与背后底层逻辑(二)https://developer.aliyun.com/article/1617342
三、vector.h
#pragma once #include <assert.h> #include <iostream> #include <algorithm> using namespace std; namespace bit { template<class T> class vector { public: typedef T* iterator; typedef const T* const_iterator; iterator begin() { return _start; } iterator end() { return _finish; } const_iterator begin() const { return _start; } const_iterator end() const { return _finish; } //构造函数 //无参构造 vector() {} //有参构造 //..... //析构 ~vector() { delete[]_start; _start = nullptr; _finish = _end_of_storage = nullptr; } //vector<int> v1={1,2,3,4,5,6}; vector(initializer_list<T> il) { reserve(il.size()); for (auto& e : il) { push_back(e); } } //拷贝构造 //v2(v1) vector(const vector<T>& v) { //需要开一样大的空间 reserve(v.capacity()); for (auto& e : v) { push_back(e); } } //v2 = v1; vector<T>& operator=(vector<T> v) { swap(v); return *this; } //通过下标访问 //并且不知道会返回什么具体的类型 T& operator[](size_t pos) { //不能等于size(),指向\0 assert(pos < size()); return _start[pos]; } const T& operator[](size_t pos) const { assert(pos < size()); return _start[pos]; } //交换 void swap(vector<T>& v) { std::swap(_start, v._start); std::swap(_start, v._start); std::swap(_start, v._start); } //得到当前元素空间信息 //指针-指针 size_t size() const { return _finish - _start; } size_t capacity() const { return _end_of_storage - _start; } //扩容 //第一个问题,迭代器失效 //void reserve(size_t n) //{ // if (n > capacity()) // { // T* tmp = new T[n]; // memcpy(tmp, _start, size(T) * n); // delete[] _start; // _start = tmp; // _finish = tmpt + size(); // _end_of_storage = tmp + n; // } //} 改良 第二个问题,当T为自定义类型,就是浅拷贝,但是还是指向同一块空间 //void reserve(size_t n) //{ // if (n > capacity()) // { // size_t old_size = size(); // T* tmp = new T[n]; // memcpy(tmp, _start, size(T) * n); // delete[] _start; // _start = tmp; // _finish = tmp + old_size; // _end_of_storage = tmp + n; // } //} 正确写法 void reserve(size_t n) { if (n > capacity()) { size_t old_size = size(); T* tmp = new T[n]; //memcpy(tmp, _start, size(T) * n); for (size_t i = 0; i < old_size; i++) { tmp[i] = _start[i]; } delete[] _start; _start = tmp; _finish = tmp + old_size; _end_of_storage = tmp +n ; } } //vector的迭代区间 //函数模板 template<class InputIterator> vector(InputIterator fist, InputIterator last) { while (fist != last) { push_back(*fist); ++fist; } } //初始化 构造 vector(size_t n, const T& val = T()) { reserve(n); for (size_t i = 0; i < n; i++) { push_back(val); } } vector(int n, const T& val = T()) { reserve(n); for (int i = 0; i < n; i++) { push_back(val); } } //重点实现resize void resize(size_t n, const T& val = T()) { //缩容 if (n <= size()) { _finish = _start + n; } else { //提前扩容 //大于capacity才起作用 reserve(n); while (_finish < _start + n) { *_finish = val; _finish++; } } } //核心操作 void push_back(const T& val ) { insert(end(), val); } void pop_back() { /* assert(!empty()); _finish--;*/ //不能-- erase(end() - 1); } //判断空 bool empty() { return _start == _finish; } //实现插入操作时,可能会涉及到扩容操作,就有可能出现迭代器失效的可能 void insert(iterator pos, const T& val ) { assert(_start <= pos); assert(pos <= _finish); //考虑是否需要扩容 //说明空间已经满了 if (_finish == _end_of_storage) { size_t len = pos - _start; //这里迭代器失效的问题 //不是解决了吗!当时pos没有解决呀! reserve(capacity() == 0 ? 4 : capacity() * 2); pos = _start + len; } //开始移动数据 iterator it = _finish - 1; while (it >= pos) { *(it + 1) = *it; it--; } *pos = val; _finish++; } //迭代器实现erase iterator erase(iterator pos) { assert(_start <= pos); //删除不到_finish这个位置 assert(pos < _finish); iterator it = pos+1; //it=pos+1=_finish就是最后一个位置 while (it <= _finish) { *(it - 1) = *(it); it++; } _finish--; return pos; } private: iterator _start=nullptr; iterator _finish=nullptr; iterator _end_of_storage=nullptr; }; // 函数模板 template<class T> void print_vector(const vector<T>& v) { //for (size_t i = 0; i < v.size(); i++) //{ // cout << v[i] << " "; //} //cout << endl; //typename vector<T>::const_iterator it = v.begin(); auto it = v.begin(); while (it != v.end()) { cout << *it << " "; ++it; } cout << endl; /*for (auto e : v) { cout << e << " "; } cout << endl;*/ } void test1() { vector<int> v; v.push_back(12); v.pop_back(); v.push_back(1); print_vector(v); } void test_vector2() { vector<int> v1(10, 1); print_vector(v1); vector<int> v2(10u, 1); print_vector(v2); vector<int> v3(10, 'a'); print_vector(v3); } void test_vector3() { vector<int> v1; v1.push_back(1); v1.push_back(2); v1.push_back(3); v1.push_back(4); v1.push_back(5); v1.push_back(6); v1.push_back(7); v1.push_back(8); print_vector(v1); vector<int>::iterator it = v1.begin() + 3; v1.insert(it, 40); print_vector(v1); cout << *it << endl; } void test_vector4() { auto x = { 1,2,3,4,5,6 }; cout << typeid(x).name() << endl; cout << sizeof(x) << endl; } void test_vector5() { vector<int> v1; v1.push_back(1); v1.push_back(2); v1.push_back(3); v1.push_back(4); v1.push_back(5); v1.push_back(4); //std::vector<int>::iterator it = v1.begin(); vector<int>::iterator it = v1.begin(); while (it != v1.end()) { if (*it % 2 == 0) { it=v1.erase(it); } else { it++; } } print_vector(v1); } }
以上就是本篇文章的所有内容,在此感谢大家的观看!这里是店小二呀C++笔记,希望对你在学习C++语言旅途中有所帮助!