前言
前几期讲过类,构造函数,运算符重载,这一期就利用这三块的知识做一期简单的应用,用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];
}