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

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

如何重载operator[]   及其相关细节

如何使用 const_cast<>(  )  和 static_cast<>( )

模板类 如何内部声明,外部定义友元函数

使用memset( )、memcpy_s( )

使用sizeof( )

禁用移动构造 和 移动赋值

1 #pragma once  2 #include 
3 using std::ostream; 4 using std::cout; 5 using std::endl; 6 7 template
8 class vector 9 { 10 public: 11 explicit vector(int size); 12 vector(const vector
& v); 13 vector(vector
&&) = delete; 14 vector(int size, T vlaue); 15 vector(T* es, int len); 16 vector& operator=(const vector
& v); 17 vector& operator=(vector
&&) = delete; 18 19 int size() const; 20 int capacity() const; 21 void reserve(int min_capacity); 22 void push_back(T e); 23 void pop_back(); 24 void erase(int pos); 25 void insert(T e, int pos); 26 27 const T& operator[](int) const; 28 T& operator[](int); 29 template
30 friend ostream& operator<<(ostream& os, const vector
& v); 31 private: 32 T* vec; 33 int _size; 34 int _capacity; 35 }; 36 37 template
38 inline ostream& operator<<(ostream& os, const vector
& v) 39 { 40 os << endl << "输出: " << endl; 41 for (int i = 0; i < v._size; i++) 42 { 43 os << v.vec[i] << " "; 44 } 45 return os; 46 } 47 48 template
49 inline vector
::vector(int size) : _size(size) 50 { 51 _capacity = size + size / 3; 52 this->vec = new T[_capacity]; 53 memset(this->vec, 0x00, sizeof(T) * _capacity); 54 } 55 56 template
57 inline vector
::vector(int size, T vlaue) 58 { 59 this->_capacity = size + size / 3; 60 this->_size = size; 61 this->vec = new T[_capacity]; 62 for (int i = 0; i < _size; i++) 63 { 64 this->vec[i] = vlaue; 65 } 66 } 67 68 template
69 inline vector
::vector(T* es, int len) 70 { 71 this->_size = len; 72 this->_capacity = len + len / 3; 73 this->vec = new T[_capacity]; 74 memcpy_s(this->vec, len * sizeof(T), es, len * sizeof(T)); 75 } 76 77 template
78 inline vector
::vector(const vector& v) 79 { 80 this->_capacity = v._capacity; 81 this->_size = v._size; 82 this->vec = new T[_capacity]; 83 memcpy_s(this->vec, sizeof(T) * _capacity, v.vec, sizeof(T) * _capacity); 84 } 85 86 template
87 inline int vector
::size() const 88 { 89 return this->_size; 90 } 91 92 template
93 inline int vector
::capacity() const 94 { 95 return this->_capacity; 96 } 97 98 template
99 inline vector
& vector
::operator=(const vector
& v)100 {101 if (this == &v)return *this;102 if (this->vec != nullptr)delete[] this->vec;103 this->_capacity = v._capacity;104 this->_size = v._size;105 this->vec = new T[_capacity];106 memcpy_s(this->vec, _capacity * sizeof(T), v.vec, v._capacity * sizeof(T));107 return *this;108 }109 110 template
111 inline void vector
::reserve(const int min_capacity)112 {113 if (min_capacity < this->_capacity) return;114 else115 {116 int* n_vec = new T[min_capacity];117 for (int i = 0; i < _size; i++)118 {119 n_vec[i] = vec[i];120 }121 delete[] vec;122 this->vec = n_vec;123 this->_capacity = min_capacity;124 }125 }126 127 template
128 inline void vector
::push_back(T e)129 {130 if (this->_size >= this->_capacity)131 {132 this->reserve(this->_size + this->_size / 3);133 }134 this->vec[_size++] = e;135 }136 137 template
138 inline void vector
::pop_back()139 {140 if (this->_size != 0)141 this->_size -= 1;142 }143 144 template
145 inline void vector
::erase(int pos)146 {147 if (pos < 0 || pos >= _size)return;148 else149 {150 for (int i = pos; i < _size - 1; i++)151 {152 this->vec[i] = this->vec[i + 1];153 }154 _size -= 1;155 }156 }157 158 template
159 inline void vector
::insert(T e, int pos)160 {161 if (pos < 0 || pos > _size - 1)return;162 if (this->_size >= this->_capacity)163 {164 this->reserve(this->_size + this->_size / 3);165 }166 for (int i = _size; i > pos; i++)167 {168 this->vec[i] = this->vec[i - 1];169 }170 this->vec[pos] = e;171 }172 173 template
174 inline const T& vector
::operator[](int i) const175 {176 if (i < 0 || i >= this->_size)177 {178 cout << "下标 i=" << i << " 越界 退出";179 exit(1);180 };181 return this->vec[i];182 }183 184 template
185 T& vector
::operator[](int pos)186 {187 // 这个写法的意思是 将指针this 转成 const 型指针 再对其*解引 188 //return const_cast
((*static_cast
*>(this))[pos]);189 190 //这个是先对this 指针解引 再转成const 而且&直接引用该对象 191 return const_cast
(static_cast
&>(*this)[pos]);192 193 //这个写法不不可取;先对this指针 解引,再转成const时 并不是&,所以会重新创建一个新对象194 //return const_cast
(static_cast
>(*this)[pos]);195 }

 

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

你可能感兴趣的文章
hibernate常见错误
查看>>
FIN_WAIT_2状态解释
查看>>
ios 不同sdk4.3 6.0版本号,关于方法的兼容性的通用方法
查看>>
Shell编程学习总结
查看>>
070、如何定制Calico 网络policy(2019-04-15 周一)
查看>>
LeetCode算法题-Reverse String(Java实现)
查看>>
构建之法阅读笔记02
查看>>
线程安全与锁优化
查看>>
Storm学习
查看>>
【重学计算机】机组D5章:指令系统
查看>>
json对象函数的好处
查看>>
Testlink解决大用例导入问题
查看>>
Webstorm常用快捷键备忘
查看>>
nginx 的windows 基本配置
查看>>
js滚动加载到底部
查看>>
关于mac远程链接window服务器以及实现共享文件
查看>>
angular的service与factory
查看>>
Redis慢查询,redis-cli,redis-benchmark,info
查看>>
新建MVC3 编译出现 System.Web.Mvc.ModelClientValidationRule
查看>>
mysql主从同步从库同步报错
查看>>