目录
一、vector的数据结构
二、vector的构造
三、vector的增删查改及空间管理
四、全部代码
一、vector的数据结构
vector以线性连续空间为基础来定义数据结构以及扩展功能。vector的两个迭代器,分别是start和finish,分别指向配置得来的已被使用的空间。还有一个迭代器,end_of_storage指向整块连续空间的尾端。
iterator _start = nullptr;
iterator _finish = nullptr;
iterator _endofstorage = nullptr;
(此处迭代器变量名前加‘_'表示我们不是真正的vector而是模拟出来的)
这些迭代器应该被private所修饰,那么,可以设计如下构造函数来提取vector的首尾,这样既保护了迭代器,又便于提取首位:
iterator begin()
{return _start;
}iterator end()
{return _finish;
}const_iterator begin() const
{return _start;
}const_iterator end() const
{return _finish;
}
vector的实际配置大小要比需求量大一些,以便将来可以扩充。也就是说,vector的容量大小永远大于或者等于其大小。一旦其容量等于其大小,即是满载,当有新的元素加入时,vector就要进行扩容。
示意图如下:
二、vector的构造
vector的构造如下:
(constructor)构造函数声明 | 接口说明 |
vector(); | 无参构造 |
vector(size_type n, const value_type& val = value_type()); | 构造并初始化n个val |
vector (const vector& x); | 拷贝构造 |
vector (InputIterator first, InputIterator last); | 使用迭代器进行初始化构造 |
我们来一一实现。
无参构造:
vector()
{}
初始化n个构造:
vector(size_t n, const T& val = T())
{resize(n, val);
}vector(int n, const T& val = T())
{resize(n, val);
}
拷贝构造:
vector(const vector<T>& v)
{_start = new T[v.capacity()];//memcpy(_start, v._start, sizeof(T)*v.size());for (size_t i = 0; i < v.size(); i++){_start[i] = v._start[i];}_finish = _start + v.size();_endofstorage = _start + v.capacity();
}
使用迭代器初始化构造:
template<class InputIterator>
vector(InputIterator first, InputIterator last)
{while (first != last){push_back(*first);++first;}
}
除此之外,也可以重载=来实现构造,原理同拷贝构造:
vector<T>& operator=(vector<T> v)
{swap(v);return *this;
}
最后,既然有构造函数,那必然有析构函数呀:
~vector()
{if (_start){delete[] _start;_start = _finish = _endofstorage = nullptr;}
}
三、vector的增删查改及空间管理
vector的增删查改功能函数如下:
vector增删查改 | 接口说明 |
push_back | 尾插 |
pop_back | 尾删 |
find | 查找。(注意这个是算法模块实现,不是vector的成员接口) |
insert | 在position之前插入val |
erase | 删除position位置的数据 |
swap | 交换两个vector的数据空间 |
operator[] | 像数组一样访问 |
要实现push_back,我们先实现insert:
iterator insert(iterator pos, const T& x)
{assert(pos >= _start && pos <= _finish);if (_finish == _endofstorage){size_t len = pos - _start;size_t newcapacity = capacity() == 0 ? 4 : capacity() * 2;reserve(newcapacity);pos = _start + len;}iterator end = _finish - 1;while (end >= pos){*(end + 1) = *end;--end;}*pos = x;++_finish;return pos;
}
这样,在设计push_back时,直接调用insert函数就好:
void push_back(const T& x)
{insert(end(), x);
}
要实现pop_back,不妨参考push_back的实现过程,先实现erase:
iterator erase(iterator pos)
{assert(pos >= _start && pos < _finish);iterator it = pos + 1;while (it != _finish){*(it - 1) = *it;++it;}--_finish;return pos;
}
再直接调用erase即可实现pop_back:
void pop_back()
{erase(--end());
}
要交换两个vector的数据空间的话,把关键迭代器交换即可:
void swap(vector<T>& v)
{std::swap(_start, v._start);std::swap(_finish, v._finish);std::swap(_endofstorage, v._endofstorage);
}
operator[]的实现如下:
T& operator[](size_t pos)
{assert(pos < size());return _start[pos];
}const T& operator[](size_t pos) const
{assert(pos < size());return _start[pos];
}
vector的空间管理功能如下:
容量空间 | 接口说明 |
size | 获取数据个数 |
capacity | 获取容量大小 |
empty | 判断是否为空 |
resize | 改变vector的size |
reserve | 改变vector的capacity |
前三个都很简单,返回相应的值即可:
size_t size() const
{return _finish - _start;
}size_t capacity() const
{return _endofstorage - _start;
}bool empyt() const
{return ((_endofstorage - _start) == 0 ? true : false);
}
重点实现的是resize和reserve:
resize如下:
void resize(size_t n, const T& val = T())
{if (n < size()){_finish = _start + n;}else{reserve(n);while (_finish != _start + n){*_finish = val;++_finish;}}
}
reserve如下:
void reserve(size_t n)
{if (n > capacity()){size_t sz = size();T* tmp = new T[n];if (_start){for (size_t i = 0; i < sz; i++){tmp[i] = _start[i];}delete[] _start;}_start = tmp;_finish = _start + sz;_endofstorage = _start + n;}
}
四、全部代码
全部代码如下:
#include<assert.h>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(size_t n, const T& val = T()){resize(n, val);}vector(int n, const T& val = T()){resize(n, val);}template<class InputIterator>vector(InputIterator first, InputIterator last){while (first != last){push_back(*first);++first;}}vector(){}vector(const vector<T>& v){_start = new T[v.capacity()];for (size_t i = 0; i < v.size(); i++){_start[i] = v._start[i];}_finish = _start + v.size();_endofstorage = _start + v.capacity();}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;}}void reserve(size_t n){if (n > capacity()){size_t sz = size();T* tmp = new T[n];if (_start){for (size_t i = 0; i < sz; i++){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{reserve(n);while (_finish != _start + n){*_finish = val;++_finish;}}}void push_back(const T& x){insert(end(), x);}void pop_back(){erase(--end());}size_t capacity() const{return _endofstorage - _start;}size_t size() const{return _finish - _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];}iterator insert(iterator pos, const T& x){assert(pos >= _start && pos <= _finish);if (_finish == _endofstorage){size_t len = pos - _start;size_t newcapacity = capacity() == 0 ? 4 : capacity() * 2;reserve(newcapacity);// 解决pos迭代器失效问题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;};
}