使用类模板实现STL Vector,点击查看代码
#include <iostream>
using namespace std; template<typename T>
class MyVector { public: //构造函数
MyVector<T>(int size = 10) {
T * _tep = new T[size]();
first = _tep;
last = _tep;
end = first + size;//
cout << "构建Vector,首地址" << first << endl;
} //拷贝构造
MyVector<T>(const MyVector<T> & _src) { //对方vector是为空
if (_src.Empty()) {
int srcVectorySize = _src.getVectorSize();
T * _tep = new T[srcVectorySize]();
first = _tep;
last = _tep;
end = first + srcVectorySize;//
cout << "拷贝构造构建Vector,空拷贝" << endl;
}
else { int srcVectorySize = _src.getVectorSize();
T * _tep = new T[srcVectorySize]();
first = _tep;
last = _tep;
end = first + srcVectorySize;//
T * _srcVectorElementPoint = _src.first;
while (_srcVectorElementPoint < _src.last) {
*_tep = *_srcVectorElementPoint;
_tep++; _srcVectorElementPoint++;
}//end
last=_tep;
cout << "拷贝构造构建Vector" << endl;
} } //赋值函数
MyVector<T> & operator=(const MyVector<T> & _src)
{
//避免重复
if (this == &_src) { return *this; } //释放现有堆上资源空间
if (this->Empty() == false) {
delete[]first;
first = nullptr;
last = nullptr;
end = nullptr;
} int srcVectorySize = _src.getVectorSize();
T * _tep = new T[srcVectorySize]();
first = _tep;
last = _tep;
end = first + srcVectorySize;//
T * _srcVectorElementPoint = _src.first;
while (_srcVectorElementPoint < _src.last) {
*_tep = *_srcVectorElementPoint;
_tep++;
_srcVectorElementPoint++;
}//end
last = _tep;
cout << "赋值函数构建Vector" << endl;
} //析构函数
~MyVector<T>() {
if (Empty() == false) {
delete[]first;
first = nullptr;
last = nullptr;
end = nullptr;
cout << "析构Vector,堆地址"<<first << endl;
} } //添加值,返回指向当前元素的指针,返回为 const * 不允许修改
const T * addValue(const T & _srcValue) { //满空间,两倍扩容
if (Full()) {
Expend();
}
*last = _srcValue;
last++;
cout << "Vector添加元素,元素地址" << last << endl; return last; } //获取指定下标的值
T getValue(int index) const {
if (index<0) { return *first; }
if (index > this->getVectorSize()) { return *(last-1); }
int flag = 0;
T *elementPoint = first;
while (flag < index) {
elementPoint++;
flag++;
}
cout << "获取Vector元素值,元素地址" << elementPoint <<"元素值"<< *elementPoint << endl;
return *elementPoint;
} //编辑指定下标元素的值,返回当前节点的指针 不允许通过返回指针修改
const T * eidtValue(int index,const T & _value) { if (index > this->getVectorSize() || index<0) { return nullptr; }
int flag = 0;
T *elementPoint = first;
while (flag < index) {
elementPoint++;
flag++;
}
*elementPoint = _value;
cout << "编辑Vector元素值,元素地址" << elementPoint << "元素值" << *elementPoint << endl;
return elementPoint;
} //判断是否为空
bool Empty() const {
if (first == last) { return true; }
return false;
}
//判断空间是否满
bool Full() const{
if (last == end) { return true; }
return false;
} int getVectorSize() const {
return this->end - this->first;
} void printVector() const { cout << "打印数组元素" << endl;
T *elementPoint = first;
int index = 0; while (elementPoint < last)
{
cout.precision(4);
cout << "[" << index << "]=" << (*elementPoint) <<"该元素地址="<< elementPoint << endl;
elementPoint++;
index++;
}
} private:
T * first;
T * last;
T * end; //两倍扩容
void Expend() { int size = this->getVectorSize(); int newSize = size * 2;
T *newFirst = new T[newSize];
T *newLast = newFirst;
T *newEnd = newFirst + newSize;
const T *srcElementPoint = this->first;
while (srcElementPoint < this->last) {
*newLast = *srcElementPoint;
newLast++;
srcElementPoint++;
} //释放原有空间
delete[]first;
first = nullptr;
last = nullptr;
end = nullptr; first = newFirst;
last = newLast;
end = newEnd; cout << "两倍扩容新堆内存地址"<< first << endl;
} }; int main() { MyVector<int> v1 (6) ; v1.addValue(10);
v1.addValue(9);
v1.addValue(8);
v1.addValue(7);
v1.addValue(6);
v1.addValue(5); v1.printVector();
v1.addValue(4.0);
v1.printVector(); v1.eidtValue(2, 100);
v1.printVector(); int getValue = v1.getValue(3); cout << "getValue =" << getValue << endl; MyVector<int> v2 = v1; v2.printVector(); MyVector<int> v3(10);
v3 = v1;
v3.printVector();
system("pause"); return 1;
}

最新文章

  1. xcode中info.plist文件相关问题
  2. [自动运维]weblogic自动发布
  3. 纯css用图片代替checkbox和radio,无js实现方法
  4. spring.net使用
  5. 使用solrj操作solr索引库
  6. adt安装慢解决
  7. 解决background图片拉伸问题
  8. Android 手势锁的实现 让自己的应用更加安全吧
  9. python学习05
  10. docker(一) Centos7下安装docker
  11. python 数据分类汇总
  12. Code First的实体继承模式
  13. MySQL InnoDB加锁超时回滚机制(转)
  14. Docker ssh server
  15. dp练习(4)——过河卒
  16. hdu 1597(矩阵快速幂)
  17. 在ASP.Net和IIS中删除不必要的HTTP响应头[转]
  18. docker使用阿里云镜像仓库
  19. android httpclient 发送 PATCH 请求
  20. FineReport——发送邮件

热门文章

  1. 【Shashlik.EventBus】.NET 事件总线,分布式事务最终一致性
  2. Python数据科学手册-Pandas数据处理之简介
  3. Nginx 动态压缩与静态压缩,显著提高前后端分离项目响应速度!
  4. nginx反向代理Grafana
  5. Elasticsearch索引生命周期管理探索
  6. 分步骤讲解Deployment故障排除
  7. Visual Studio 2022 开发 STM32 单片机 - 环境搭建点亮LED灯
  8. Linux+Wine玩GTA5指南
  9. PHP全栈开发(八):CSS Ⅶ 表格 style
  10. Spring MVC(配置、入门)