C++封装顺序表


前言

前几期讲过类,构造函数,运算符重载,这一期就利用这三块的知识做一期简单的应用,用C++封装顺序表。


一、顺序表是什么?

在这里插入图片描述
如图所示,在内存中的一块连续的空间
这种内存布局非常像数组,因此也被成为动态数组

在C++标椎模板库中有一个顺序表vector容器,我们要做的就是封装一个丐版的vector容器

二、需求分析

1.定义Myvector类,和内部接口

2.初始化、销毁、深拷贝

3.自动扩容

4.增删查改

5.获取大小和容量

6.用 [ 索引 ] 访问数据

三、具体实现

1.定义Myvector类,和内部接口

template<typename Type>
class Myvector
{
protected:
	Type* data;
	int _size;  //容器元素个数
	int _capacity;//容量
public:
	/*所有接口*/
	Myvector();//初始化
	Myvector(Myvector& vec);//拷贝构造
	~Myvector();//容器销毁,内存回收
	void EnlargedCapacity();//扩容
	Myvector& pushback(Type data);//尾插
	Myvector& pushfront(Type data);//头插
	Myvector& popback();//尾删
	Myvector& popfront();//头删
	int size();//获取元素个数
	int capacity();//获取容量大小
	Type operator[](Type indexes);// [] 运算符重载,可用索引访问数据
	Myvector& operator=(Myvector& vec);// = 运算符重载,拷贝赋值;
};

2.初始化、销毁、深拷贝

template<typename Type>
Myvector<Type>::Myvector()//初始化
{
	_size = 0;
	_capacity = 4;
	data = new Type[_capacity];
}

template<typename Type>
Myvector<Type>::Myvector(Myvector<Type>& vec)//拷贝构造
{
	this->data = new Type[vec._size];
	memcpy(this->data, vec.data, _size * sizeof(Type));
	this->_capacity = vec._capacity;
	this->_size = vec._size;
}

template<typename Type>
Myvector<Type>& Myvector<Type>::operator=(Myvector& vec)// = 运算符重载,拷贝赋值;
{
	this->data = new Type[vec._size];
	memcpy(this->data, vec.data, _size * sizeof(Type));
	this->_capacity = vec._capacity;
	this->_size = vec._size;
	return *this;
}

template<typename Type>
Myvector<Type>::~Myvector()//释放内存
{
	delete[] data;
	data = NULL;
	_size = 0;
	_capacity = 0;
}

3.自动扩容

template<typename Type>
void Myvector<Type>::EnlargedCapacity()
{
	if (_size >= _capacity)
	{
		_capacity *= 2;
		int* p = new Type[_capacity];
		memcpy(p, data, _size * sizeof(Type));
		delete[] data;
		data = p;
		p = nullptr;
	}
}

4.增删查改

template<typename Type>
Myvector<Type>& Myvector<Type>::pushback(Type data)//尾插
{
	EnlargedCapacity();
	this->data[_size] = data;
	_size++;
	return *this;
}

template<typename Type>
Myvector<Type>& Myvector<Type>::popback()//尾删
{
	_size--;
	return *this;
}

template<typename Type>
Myvector<Type>& Myvector<Type>::pushfront(Type data)//头插
{
	EnlargedCapacity();
	for (int i = 0; i < _size; i++)
		this->data[_size - i] = this->data[_size - i - 1];
	this->data[0] = data;
	return *this;
}

template<typename Type>
Myvector<Type>& Myvector<Type>::popfront()//头删
{
	_size--;
	for (int i = 0; i < _size; i++)
		this->data[i] = this->data[i + 1];
	return *this;
}

5.获取大小和容量

template<typename Type>
int Myvector<Type>::size()//获取元素个数
{
	return this->_size;
}

template<typename Type>
int Myvector<Type>::capacity()//获取容量
{
	return this->_capacity;
}

6.用 [ 索引 ] 访问数据

template<typename Type>
Type Myvector<Type>::operator[](Type indexes)// [] 运算符重载,可用索引访问数据
{
	return this->data[indexes];
}

全部代码:

#include<iostream>

template<typename Type>
class Myvector
{
protected:
	Type* data;
	int _size;  //容器元素个数
	int _capacity;//容量
public:
	/*所有接口*/
	Myvector();//初始化
	Myvector(Myvector& vec);//拷贝构造
	~Myvector();//容器销毁,内存回收
	void EnlargedCapacity();//扩容
	Myvector& pushback(Type data);//尾插
	Myvector& pushfront(Type data);//头插
	Myvector& popback();//尾删
	Myvector& popfront();//头删
	int size();//获取元素个数
	int capacity();//获取容量大小
	Type operator[](Type indexes);// [] 运算符重载,可用索引访问数据
	Myvector& operator=(Myvector& vec);// = 运算符重载,拷贝赋值;
};

template<typename Type>
Myvector<Type>::Myvector()
{
	_size = 0;
	_capacity = 4;
	data = new Type[_capacity];
}

template<typename Type>
Myvector<Type>::Myvector(Myvector<Type>& vec)//拷贝构造
{
	this->data = new Type[vec._size];
	memcpy(this->data, vec.data, _size * sizeof(Type));
	this->_capacity = vec._capacity;
	this->_size = vec._size;
}

template<typename Type>
Myvector<Type>& Myvector<Type>::operator=(Myvector& vec)// = 运算符重载,拷贝赋值;
{
	this->data = new Type[vec._size];
	memcpy(this->data, vec.data, _size * sizeof(Type));
	this->_capacity = vec._capacity;
	this->_size = vec._size;
	return *this;
}

template<typename Type>
Myvector<Type>::~Myvector()
{
	delete[] data;
	data = NULL;
	_size = 0;
	_capacity = 0;
}

template<typename Type>
void Myvector<Type>::EnlargedCapacity()
{
	if (_size >= _capacity)
	{
		_capacity *= 2;
		int* p = new Type[_capacity];
		memcpy(p, data, _size * sizeof(Type));
		delete[] data;
		data = p;
		p = nullptr;
	}
}

template<typename Type>
Myvector<Type>& Myvector<Type>::pushback(Type data)//尾插
{
	EnlargedCapacity();
	this->data[_size] = data;
	_size++;
	return *this;
}

template<typename Type>
Myvector<Type>& Myvector<Type>::popback()//尾删
{
	_size--;
	return *this;
}

template<typename Type>
Myvector<Type>& Myvector<Type>::pushfront(Type data)//头插
{
	EnlargedCapacity();
	for (int i = 0; i < _size; i++)
		this->data[_size - i] = this->data[_size - i - 1];
	this->data[0] = data;
	return *this;
}

template<typename Type>
Myvector<Type>& Myvector<Type>::popfront()//头删
{
	_size--;
	for (int i = 0; i < _size; i++)
		this->data[i] = this->data[i + 1];
	return *this;
}

template<typename Type>
int Myvector<Type>::size()//获取元素个数
{
	return this->_size;
}

template<typename Type>
int Myvector<Type>::capacity()//获取容量
{
	return this->_capacity;
}

template<typename Type>
Type Myvector<Type>::operator[](Type indexes)// [] 运算符重载,可用索引访问数据
{
	return this->data[indexes];
}

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用c++实现的顺序:多文件编程,层次清晰,函数有注释 SeqList();//构造函数,存储的元素个数设为0 bool setLength(size_t length);//设置已经存储的元素个数 bool addElement(ElemType element);//把某个元素添加到顺序末尾 bool addElement(ElemType element , size_t n);//插入一个元素,使其成为第n个元素,其余元素后移 bool delElement();//删除所有的元素 bool delElement(size_t n);//删除第n个元素 bool delElement(string elementDetailType,string elementDetail);//通过某个元素细节找到元素,把这个元素删除 bool replaceElement(ElemType element , size_t n);//使用一个元素,替换掉第n个元素 bool swapElement(size_t n1 , size_t n2);//把第n1个元素和第n2个元素交换 ElemType* getElement();//得到数组头的指针 ElemType* getElement(size_t n);//得到第n个元素的指针 size_t getLength();//得到存储的元素个数 size_t getMaxSize();//得到顺序容量 bool showElementDetail();//输出所有的元素细节 bool showElementDetail(size_t n);//输出第n个元素的细节 bool showElementDetail(string elementDetailType,string elementDetail);//通过某个元素细节找到元素,输出元素所有细节 size_t findElement(string elementDetailType,string elementDetail);//通过某个元素细节找到元素位置 static int inputAInt(int min = 0,int max = 9,int defaultValue = -1);//从键盘读取,限制为一个min到max间的整数,非法情况返回defaultValue void startControlLoop();//打开控制界面 ~SeqList();//析构函数

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值