ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

vector类的简单实现

2021-04-13 18:35:21  阅读:145  来源: 互联网

标签:const 实现 theSize int vector 简单 array other theCapacity


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

  执行结果:

 

标签:const,实现,theSize,int,vector,简单,array,other,theCapacity
来源: https://www.cnblogs.com/Christal-R/p/14654469.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有