当前位置: 首页 > news >正文

vector的简单模拟实现_C++

目录

一、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;};
}

http://www.lryc.cn/news/240265.html

相关文章:

  • 合并两个有序链表,剑指offer,力扣
  • Delphi 12 Athens 发布了!
  • 基于Haclon的Blob分析
  • 安卓手机好用的清单软件有哪些?
  • 【追求卓越02】数据结构--链表
  • qt按照不同编码格式读取文字(UTF-16LE,UTF-8,UTF-8BOM,UTF-16BE)
  • R语言和RStudio的下载安装(非常简便舒适)
  • SQL注入漏洞发现和利用,以及SQL注入的防护
  • Jmeter 分布式压测
  • Docker 安装 Apache
  • python变量、常量、数据类型
  • 注册中心CAP架构剖析
  • SVN创建分支
  • Vue 设置v-html中元素样式
  • 连接服务器的脚本
  • ChatGPT/GPT4丨编程助手;AI画图;数据分析;科研/项目实现;提示词工程技巧;论文写作等
  • 35的程序员被辞了可以自己接外包啊?为什么都那么悲观呢?
  • 2020年09月 Scratch(三级)真题解析#中国电子学会#全国青少年软件编程等级考试
  • SpringBoot面试之SpringBoot自动装配原理
  • JavaScript:监听事件
  • 编写SQL语句,场景:从一张表中查询某字段是逗号分隔的集合值,需要遍历集合内每个值,将其作为条件去查询另一张表,最终返回列表
  • 单链表相关面试题--7.链表的回文结构
  • JUC(Java Util Concurrent)多线程并发库
  • 如何在Linux系统上检测GPU显存和使用情况?
  • Django 入门学习总结5
  • FileNotFoundError: [Errno 2] No such file or directory: ‘patchelf‘: ‘patchelf‘
  • 『new Date 在 IOS 失效 の bug』
  • macos创建xcframework及签名
  • Oracle与Redis Enterprise协同,作为企业缓存解决方案
  • 局部保持投影(Locality preserving projections,LPP)