vector就是我们之前数据结构学的顺序表,这篇博客就是说一说它的简单使用和底层实现
文章目录
- 简单使用
- 模拟实现
简单使用
首先,我们看看它的构造函数
我们比较常用的也就是第二种,就是第一个参数是要存的数据个数,第二个是要填充的数据
vector<int>f(10, 0);
这个的意思就是表中存放十个数据,每个数据是0
vector<int>g;
这种就是创建一个空的vector表,也就对应上面第一种情况
除了这两种,第三种情况可以看到是一个模板,这个模板实例化后可以是迭代器,也就是用迭代器区间去构造,当然如果是数组也可以,因为它的底层空间是连续的
int main() {vector<int>f(10, 0);vector<int>g(f.begin(), f.end());for (auto e : g) {cout << e << ' ';}cout << endl;string s1("abcdefg");vector<char>s2(s1.begin(), s1.end());for (auto e : s2) {cout << e << ' ';}cout << endl;return 0;
}
char s1[] = "abcdefg";vector<char>s2(s1,s1+6);for (auto e : s2) {cout << e << ' ';}cout << endl;
这里就是分别用迭代器和数组名(指针)初始化的。
跟string一样,vector的数据访问也可以用方括号,迭代器和范围for
int main() {vector<int>f;f.push_back(1);f.push_back(2);f.push_back(3);f.push_back(4);f.push_back(5);for (size_t i = 0; i < f.size(); i++) {cout << f[i] << ' ';}cout << endl;vector<int>::iterator it = f.begin();while (it != f.end()) {cout << *it << ' ';it++;}cout << endl;for (auto e : f) {cout << e << ' ';}cout << endl;return 0;
}
我们这里插入函数传位置的话只能传迭代器,我们可以先查找一下,查找就用算法里的find
要包含算法的头文件
int main() {vector<int>f;f.push_back(1);f.push_back(2);f.push_back(3);f.push_back(4);f.push_back(5);vector<int>::iterator it = find(f.begin(), f.end(), 3);f.insert(it, 30);for (auto e : f) {cout << e << ' ';}cout << endl;
}
模拟实现
#include<assert.h>
namespace jxh {template<class T>class vector {public:typedef T* iterator;typedef const T* const_iterator;vector() {};vector(const vector<T>& v) {reserve(v.capacity());for (const auto& e : v) {push_back(e);}}template <class InputIterator>vector(InputIterator first, InputIterator last) {while (first != last) {push_back(*first);++first;}}vector(size_t n, const T& val = T()) {resize(n, val);}vector(int n, const T& val = T()) {resize(n, val);}void swap(vector<T>& v) {std::swap(_start, v._start);std::swap(_finish, v._finish);std::swap(_endofstorage, v._endofstorage);}vector<T>& operator=(vector<T>v) {swap(v);return *this;}~vector() {if (_start) {delete[]_start;_start = _finish = _endofstorage = nullptr;}}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 _endofstorage - _start;}T& operator[](size_t pos) {assert(pos < size());return _start[pos];}const T& operator[](size_t pos)const {assert(pos < size());return _start[pos];}void reserve(size_t n) {if (n > capacity()) {size_t old = size();T* tmp = new T[n];if (_start) {for (size_t i = 0; i < old; i++) {tmp[i] = _start[i];}delete[]_start;}_start = tmp;_finish = _start + old;_endofstorage = _start + n;}}void resize(size_t n, T val = T()) {if (n > size()) {reserve(n);while (_finish < _start + n) {*_finish = val;++_finish;}}else {_finish = _start + n;}}void push_back(const T& x) {if (_finish == _endofstorage) {size_t newcapacity = capacity() == 0 ? 4 : capacity() * 2;reserve(newcapacity);}*_finish = x;++_finish;}void pop_back() {assert(size() > 0);--_finish;}iterator insert(iterator pos, const T& x) {assert(pos >= _start && 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;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;}private:iterator _start = nullptr;iterator _finish = nullptr;iterator _endofstorage = nullptr;};void print_vector(const vector<int>& v) {for (auto e : v) {cout << e << " ";}cout << endl;}
}
STL源码中,vector的实现不是像我们之前用一个指针,一个size,一个capacity实现的,而是用三个指针,用指针之间的减法来算出size和capacity