• 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;
    }
    

      执行结果:

  • 相关阅读:
    (转载) 随机数原理
    ZOJ 2588 Burning Bridges(求桥的数量,邻接表)
    生成不重复的随机数对(C/C++)
    比较两个文件是否相同(C/C++语言)
    计算文件大小(C/C++语言)
    (转载)Nim游戏博弈(收集完全版)
    将一串字符串全排列输出(回溯法)
    Linux中使用Crontab定时监测维护Tomcat应用程序的方法
    Nginx单向认证的安装配置
    非关系型数据库 2017-02-12 22:27 189人阅读 评论(2) 收藏
  • 原文地址:https://www.cnblogs.com/Christal-R/p/14654469.html
Copyright © 2020-2023  润新知