vector的模拟实现(C++)

news/2025/1/15 17:46:43/

一、构造函数

vector()   //构造函数:_start(nullptr),_finish(nullptr),_endofstorage(nullptr)
{}vector(int n, const T& val = T())//构造函数:_start(nullptr), _finish(nullptr), _endofstorage(nullptr)
{reserve(n);while (n--){push_back(val);}
}template<class InputIterator>
vector(InputIterator first, InputIterator last)//构造函数:_start(nullptr), _finish(nullptr), _endofstorage(nullptr)
{while (first != last){push_back(*first);first++;}
}

 二、拷贝构造

vector(const vector<T>& v)//拷贝构造:_start(nullptr),_finish(nullptr),_endofstorage(nullptr)
{reserve(v.capacity());for (auto& c : v){push_back(c);}
}

三、析构

~vector()
{delete[] _start;_start = _finish = _endofstorage = nullptr;
}

四、重载

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> tmp)
{swap(tmp);return *this;
}
T& operator[](size_t pos)
{assert(pos < size());return *(_start + pos);
}
const T& operator[](size_t pos) const
{assert(pos < size());return *(_start + pos);
}

五、扩容及增删

size_t size() const
{return _finish - _start;
}size_t capacity() const
{return _endofstorage - _start;
}void push_back(const T& val)
{//if (_finish == _endofstorage)//{//	reserve(capacity() == 0 ? 4 : capacity() * 2);//}//*_finish = val;//_finish++;insert(end(), val);
}void reserve(size_t n)
{if (capacity() < n){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 pop_back()
{_finish--;
}
void insert(iterator pos, const T& x)
{assert(pos >= _start);assert(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++;
}void erase(iterator pos)
{assert(pos >= _start);assert(pos < _finish);while (pos != end()){*pos = *(pos + 1);pos++;}_finish--;
}

总体:

namespace asd //设一个命名空间避免与std冲突
{template<class T>class vector{public:typedef T* iterator;typedef const T* const_iterator;vector()   //构造函数:_start(nullptr),_finish(nullptr),_endofstorage(nullptr){}vector(int n, const T& val = T())//构造函数:_start(nullptr), _finish(nullptr), _endofstorage(nullptr){reserve(n);while (n--){push_back(val);}}template<class InputIterator>vector(InputIterator first, InputIterator last)//构造函数:_start(nullptr), _finish(nullptr), _endofstorage(nullptr){while (first != last){push_back(*first);first++;}}vector(const vector<T>& v)//拷贝构造:_start(nullptr),_finish(nullptr),_endofstorage(nullptr){reserve(v.capacity());for (auto& c : v){push_back(c);}}vector<T>& operator=(vector<T> tmp){swap(tmp);return *this;}~vector(){delete[] _start;_start = _finish = _endofstorage = nullptr;}//迭代器iterator begin(){return _start;}iterator end(){return _finish;}const_iterator cbegin() const{return _start;}const_iterator cend() const{return _finish;}size_t size() const{return _finish - _start;}size_t capacity() const{return _endofstorage - _start;}void push_back(const T& val){//if (_finish == _endofstorage)//{//	reserve(capacity() == 0 ? 4 : capacity() * 2);//}//*_finish = val;//_finish++;insert(end(), val);}void reserve(size_t n){if (capacity() < n){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++;}}}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 pop_back(){_finish--;}void swap(vector<T>& v){std:: swap(_start, v._start);std::swap(_finish, v._finish);std::swap(_endofstorage, v._endofstorage);}void insert(iterator pos, const T& x){assert(pos >= _start);assert(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++;}void erase(iterator pos){assert(pos >= _start);assert(pos < _finish);while (pos != end()){*pos = *(pos + 1);pos++;}_finish--;}private:iterator _start;iterator _finish;iterator _endofstorage;};
}


http://www.ppmy.cn/news/1563384.html

相关文章

智能网联汽车技术底盘线控技术

底盘线控是通过CAN总线来进行控制的&#xff1b; CAN收发器类似于ECU或者是单片机&#xff0c;作用是用于发送高低电平&#xff0c;控制CAN的差分信号&#xff1b; 终端电阻又被称作为数据传输终端&#xff1a;防止数据以回声的形式在线段被反射&#xff1b; CAN总线如何传输…

进程同步之信号量机制

信号量机制 信号量机制是一种用于进程同步和互斥的基本工具&#xff0c;特别是在并发编程中&#xff0c;广泛用于控制对共享资源的访问&#xff0c;避免数据竞争和死锁等问题。信号量机制由荷兰计算机科学家Edsger Dijkstra在1965年提出&#xff0c;并在操作系统的进程同步中发…

分布式缓存redis

分布式缓存redis 1 redis单机&#xff08;单节点&#xff09;部署缺点 &#xff08;1&#xff09;数据丢失问题&#xff1a;redis是内存存储&#xff0c;服务重启可能会丢失数据 &#xff08;2&#xff09;并发能力问题&#xff1a;redis单节点&#xff08;单机&#xff09;部…

Python自学 - “包”的创建与使用(从头晕到了然)

<< 返回目录 1 Python自学 - “包”的创建与使用(从头晕到了然) 相对于模块&#xff0c;包是一个更大的概念&#xff0c;按照业界的开发规范&#xff0c;1个代码文件不要超过1000行&#xff0c;稍微有点规模的任务就超过这个代码限制了&#xff0c;必然需要多个文件来管…

特制一个自己的UI库,只用CSS、图标、emoji图 第二版

图&#xff1a; 代码&#xff1a; index.html <!DOCTYPE html> <html lang"zh-CN"> <head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, initial-scale1.0"><title>M…

使用 Java 操作 Excel 的实用教程

&#x1f496; 欢迎来到我的博客&#xff01; 非常高兴能在这里与您相遇。在这里&#xff0c;您不仅能获得有趣的技术分享&#xff0c;还能感受到轻松愉快的氛围。无论您是编程新手&#xff0c;还是资深开发者&#xff0c;都能在这里找到属于您的知识宝藏&#xff0c;学习和成长…

Linux(CentOS7)安装JDK和Maven

文章目录 CentOS软件安装方式JDK安装Maven安装 CentOS软件安装方式 安装方式特点二进制发布包安装软件已经针对具体平台编译打包发布&#xff0c;只要解压&#xff0c;修改配置即可。例如tomcatrpm(redhat package manager)安装软件已经按照redhat的包管理规范进行打包&#x…

.NET内网实战:反射实现Rundll32绕过防护

01阅读须知 此文所节选自小报童《.NET 内网实战攻防》专栏&#xff0c;主要内容有.NET在各个内网渗透阶段与Windows系统交互的方式和技巧。 02基本介绍 本文内容部分节选自小报童《.NET 通过反射技术实现Rundll32功能绕过安全防护》&#xff0c;目前已有280位朋友抢先预定&a…