博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
简单的 vector
阅读量:6833 次
发布时间:2019-06-26

本文共 4566 字,大约阅读时间需要 15 分钟。

1 #pragma once  2 #include 
3 #include
4 #include
5 using std::ostream; 6 using std::cout; 7 using std::endl; 8 class vector{ 9 public : 10 vector(); 11 ~vector(); 12 explicit vector(int size_); 13 vector(int size_,int e); 14 vector(const vector&a); 15 vector(int *data,int len); 16 vector& operator=(const vector&a); 17 vector(vector&&)=delete; 18 vector& operator=(vector&&)=delete; 19 20 21 int size()const; 22 int capacity()const; 23 void reserve(int capacity_); 24 void resize(int size_); 25 int& operator[](int); 26 void insert(int pos,int x);//insert a new element before the position pos 27 void erase(int pos);//erase the element at the position pos; 28 void push_back(int x);//insert a new element at the end 29 void pop_back();//erase the last element 30 int& front();//return the first element 31 const int& front()const; 32 int& back();//return the last element 33 const int& back()const; 34 bool empty()const;//true if the size is 0 35 friend ostream& operator <<(ostream&os,const vector& a){ 36 for(int i=0;i
_size-1)return false; 49 else return true; 50 } 51 }; 52 vector::vector(){ 53 _size=0; 54 _capacity=0; 55 elems=nullptr; 56 } 57 vector::~vector(){ 58 delete[] elems; 59 } 60 61 vector::vector(int size_):_size(size_),_capacity(size_){ 62 this->elems=new int[_capacity]; 63 memset(this->elems,0x3f,_capacity*4); 64 } 65 vector::vector(int size_, int e):_size(size_),_capacity(size_){ 66 this->elems=new int[_capacity]; 67 for(int i=0;i
elems[i]=e; 69 } 70 } 71 72 vector::vector(const vector &a){ 73 this->_size=a._size; 74 this->_capacity=a._capacity; 75 this->elems=new int[_capacity]; 76 memcpy_s(this->elems,_capacity*4,a.elems,_capacity*4); 77 } 78 vector::vector(int *data, int len){ 79 this->_capacity=len+len/3; 80 this->_size=len; 81 this->elems=new int[_capacity]; 82 memset(this->elems,0x3f,_capacity*4); 83 memcpy_s(this->elems,_size*4,data,len*4); 84 } 85 vector& vector::operator =(const vector&a){ 86 if(*this==a) return *this; 87 delete[] this->elems; 88 this->_size=a._size; 89 this->_capacity=a._capacity; 90 this->elems=new int[_capacity]; 91 memcpy_s(this->elems,_capacity*4,a.elems,_capacity*4); 92 return *this; 93 } 94 int vector::size()const{ 95 return this->_size; 96 } 97 int vector::capacity()const{ 98 return this->_capacity; 99 }100 void vector::reserve(int capacity_){101 if(capacity_<=this->_capacity)return;102 else{103 this->_capacity=capacity_;104 int *es=new int[_capacity];105 memset(es,0x3f,_capacity*4);106 memcpy_s(es,_size*4,elems,_size*4);107 delete[] elems;108 this->elems=es;109 }110 }111 void vector::resize(int size_){112 if(size_
_size)this->_size=size_;113 else if(size_<=this->_capacity){114 while(this->_size
elems[this->_size++]=-1;116 }117 }else if(size_>this->_capacity){118 this->reserve(size_);119 while(this->_size
_capacity){120 this->elems[this->_size++]=-1;121 }122 }123 }124 125 int& vector::operator [](int p){126 if(_safePos(p))127 return this->elems[p];128 exit(1);129 }130 void vector::insert(int pos, int x){131 if(!_safePos(pos))exit(1);132 if(this->_size==this->_capacity){133 reserve(this->_capacity+this->_size/3);134 }135 for(int j=this->_size;j>pos;j--){136 this->elems[j]=this->elems[j-1];137 }138 this->elems[pos-1]=x;139 }140 void vector::erase(int pos){141 if(!this->_safePos(pos))exit(1);142 for(int i=pos;i
_size-1;i++){143 this->elems[i]=this->elems[i+1];144 }145 this->_size-=1;146 }147 void vector::push_back(int x){148 if(this->_capacity==this->_size){149 this->reserve(this->_capacity+this->_size/3);150 this->elems[this->_size++]=x;151 }else{152 this->elems[this->_size++]=x;153 }154 }155 void vector::pop_back(){156 if(!this->empty())157 --this->_size;158 }159 int& vector::front(){160 return const_cast
(static_cast
(*this).front());//把指针转成const型,调用下面一个。再将结果的转成非const161 }162 const int& vector::front() const{163 return this->elems[0];164 }165 int& vector::back(){166 return const_cast
(static_cast
(this)->back());//这里试试两种不同的写法而已167 }168 const int& vector::back() const{169 return this->elems[_size-1];170 }171 bool vector::empty()const{172 return _size==0?true:false;173 }

 

转载于:https://www.cnblogs.com/infoo/p/7637520.html

你可能感兴趣的文章
送给自己的春节回家最佳高薪充电技术
查看>>
用什么样的个人笔记类软件?OneNote、EverNote(印象笔记)、为知笔记、麦库记事、有道云笔记……...
查看>>
运维监控利器Nagios:概念、结构和功能
查看>>
《Python从小白到大牛》第7章 运算符
查看>>
C#中程序的退出
查看>>
MDT 2013 Update 1 Preview 部署 Windows 10之批量部署实战
查看>>
数据建模在性能测试中的理解
查看>>
离开网易的转型之路1:选择测试之路-路上的迷茫
查看>>
RHEL6入门系列之三十一,管理计划任务
查看>>
CentOS 用Strongswan搭建IPSec ***
查看>>
CentOS7 安装向导
查看>>
常见病毒ACL
查看>>
Visual Studio 2015 速递(4)——高级特性之移动开发
查看>>
第三章 Shell表达式与运算符
查看>>
葡萄城报表模板库更新:新增6个行业、50张经典报表模板
查看>>
在制作WORD小报时添加艺术横线或者艺术竖线
查看>>
值得一看:一个故事说清楚锐捷网络COffice的作用和优势
查看>>
Powershell管理系列(二十六)PowerShell操作之批量导出&导入邮箱
查看>>
K8S网络NAT问题分析与处理
查看>>
XStream处理重复的或循环引用
查看>>