vector类的简单实现
2021/4/13 18:55:35
本文主要是介绍vector类的简单实现,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
vector支持很多种数据类型,故要定义成模板类
0、数据成员
- 长度 theSize
- 容量 theCapacity
- 指针 T* array
- 另外还要指定容量的增长步长
int theSize; int theCapacity; T* array; #define WALK_LENGTH 64;
1、构造函数
- 无参数构造函数
- 用几个相同值初始化的构造函数
- 拷贝构造函数
- 析构函数
myVector():theSize(0),theCapacity(0),array(NULL){} myVector(const T& target , int num):theSize(0),theCapacity(0),array(NULL) { while( num-- ) push_back(target); } myVector(const myVector<T>& other):theSize(0),theCapacity(0),array(NULL) { // 已重载本类=操作符,有开辟新空间,仍属于深拷贝 *this = other; //重载= } ~myVector() { clear(); }
2、基本成员函数
- 长度获取
- 容量获取
- 是否为空
- 清空
- 打印
int size() const { return theSize; } int capacity() const { return theCapacity; } bool empty() { return theSize==0; } void clear() { if( array ) delete array; array = NULL; theSize = 0; theCapacity = 0; } void printArray() { for( int i=0; i<theSize; i++ ) cout<<array[i]<<" "; cout<<",size="<<theSize<<" ,capacity="<<theCapacity<<endl; }
3、实现增删
- 在头插入
- 在尾插入
- 在指定位置插入
- 删除指定位置
void push_back(const T& target) { insert_before(theSize, target); } void push_front(const T& target) { insert_before(0, target); } void insert_before(const int& pos, const T& target) { if(theSize == theCapacity) { /* array没有delete之前,原来的空间仍然存在, /* 当array申请了新空间,只是失去了旧空间的指向, /* 用oldarray指向旧空间,等新空间拷贝完,再delete旧空间 */ T* oldarray = array; theCapacity += WALK_LENGTH; array = new T[theCapacity]; for( int i=0; i<theSize; i++ ) array[i] = oldarray[i]; delete oldarray; } for( int i=theSize; i>pos; i-- ) array[i] = array[i-1]; array[pos] = target; theSize++; } void erase(const int& pos) { if( pos < theSize ) { for( int i=pos; i<theSize; i++ ) array[i] = array[i+1]; theSize--; } }
4、操作符重载
- 赋值操作符 =
- 下标操作符 [ ]
myVector<T>& operator = (const myVector<T>& other) { //参数other为const 所以other调用的函数都应定义为const if( this == &other ) return *this; clear(); theSize = other.size(); theCapacity = other.capacity(); array = new T[theCapacity]; for( int i=0; i<theSize; i++ ) array[i] = other[i]; //重载[] return *this; } T& operator [] ( const int& pos ) const { assert(pos<theSize); return array[pos]; }
总结:整体代码
#include <iostream> #include <assert.h> using namespace std; template<typename T> class myVector { private: int theSize; int theCapacity; T* array; #define WALK_LENGTH 64; public: myVector():theSize(0),theCapacity(0),array(NULL){} myVector(const T& target , int num):theSize(0),theCapacity(0),array(NULL) { while( num-- ) push_back(target); } myVector(const myVector<T>& other):theSize(0),theCapacity(0),array(NULL) { // 已重载本类=操作符,有开辟新空间,仍属于深拷贝 *this = other; //重载= } ~myVector() { clear(); } myVector<T>& operator = (const myVector<T>& other) { //参数other为const 所以other调用的函数都应定义为const if( this == &other ) return *this; clear(); theSize = other.size(); theCapacity = other.capacity(); array = new T[theCapacity]; for( int i=0; i<theSize; i++ ) array[i] = other[i]; //重载[] return *this; } T& operator [] ( const int& pos ) const { assert(pos<theSize); return array[pos]; } void clear() { if( array ) delete array; array = NULL; theSize = 0; theCapacity = 0; } int size() const { return theSize; } int capacity() const { return theCapacity; } bool empty() { return theSize==0; } void push_back(const T& target) { insert_before(theSize, target); } void push_front(const T& target) { insert_before(0, target); } void insert_before(const int& pos, const T& target) { if(theSize == theCapacity) { /* array没有delete之前,原来的空间仍然存在, /* 当array申请了新空间,只是失去了旧空间的指向, /* 用oldarray指向旧空间,等新空间拷贝完,再delete旧空间 */ T* oldarray = array; theCapacity += WALK_LENGTH; array = new T[theCapacity]; for( int i=0; i<theSize; i++ ) array[i] = oldarray[i]; delete oldarray; } for( int i=theSize; i>pos; i-- ) array[i] = array[i-1]; array[pos] = target; theSize++; } void erase(const int& pos) { if( pos < theSize ) { for( int i=pos; i<theSize; i++ ) array[i] = array[i+1]; theSize--; } } void printArray() { for( int i=0; i<theSize; i++ ) cout<<array[i]<<" "; cout<<",size="<<theSize<<" ,capacity="<<theCapacity<<endl; } }; int main() { myVector<int> vec1; //无参数构造函数 cout<<"相同值赋值的构造函数"<<endl; myVector<int> vec2(2,4); vec2.printArray(); cout<<"拷贝构造函数"<<endl; myVector<int> vec3(vec2); vec3.printArray(); cout<<"在头插入 1"<<endl; vec2.push_front(1); vec2.printArray(); cout<<"删除位置11"<<endl; vec2.erase(11); vec2.printArray(); cout<<"删除位置0"<<endl; vec2.erase(0); vec2.printArray(); cout<<"在尾插入3"<<endl; vec2.push_back(3); vec2.printArray(); cout<<"在位置2之前插入4"<<endl; vec2.insert_before(2,4); vec2.printArray(); cout<<"操作符=重载"<<endl; vec1 = vec2; vec1.printArray(); return 0; }
执行结果:
这篇关于vector类的简单实现的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-22怎么通过控制台去看我的页面渲染的内容在哪个文件中呢-icode9专业技术文章分享
- 2024-12-22el-tabs 组件只被引用了一次,但有时会渲染两次是什么原因?-icode9专业技术文章分享
- 2024-12-22wordpress有哪些好的安全插件?-icode9专业技术文章分享
- 2024-12-22wordpress如何查看系统有哪些cron任务?-icode9专业技术文章分享
- 2024-12-21Svg Sprite Icon教程:轻松入门与应用指南
- 2024-12-20Excel数据导出实战:新手必学的简单教程
- 2024-12-20RBAC的权限实战:新手入门教程
- 2024-12-20Svg Sprite Icon实战:从入门到上手的全面指南
- 2024-12-20LCD1602显示模块详解
- 2024-12-20利用Gemini构建处理各种PDF文档的Document AI管道