手动撸一个c++数组类

引言:虽然数组不是c++标准库类型,但是对于我们平时解决业务上的一些问题很有帮助,这里可以自己实现自己的迷你版数组类。即实现平常使用的一些数组的封装方法,使自己对c++中数组这一类型有更加深刻的了解。

一、首先总结一下自定义数组包含的方法:

1、获取数组的大小:size();
2、获取数组的容量:capcacity();
3、尾部插入元素:push();
4、尾部删除元素:pop();
5、访问数组元素:operator[]()
//即重载访问运算符

二、首先是数组类的声明和定义:

因为使用了类模板,所以我们将声明和定义放在同一个文件中:

文件名:myArray.hpp
#pragma once
#include <iostream>
#include <string>
using namespace std;

/********************************
 *类模板的使用测试
 *手动撸一个数组(数组封装)
 ********************************/

template <class T>
class MyArray {
public:
	MyArray() = default;               //默认构造
	MyArray(const int capacity);       //自定义构造
	MyArray(const MyArray & array);    //拷贝构造
	int size() const;                  //获取当前的元素个数
	int capacity() const;              //获取当前数组的空间容量
	void push(const T &e);             //插入元素
	void pop();                        //删除尾部元素 
	T& operator[] (size_t index);      //重载访问运算符
	~MyArray();                        //默认析构
	
private:
	T *pAddress;     //在堆区开辟的空间的首地址
	int mSize;       //数组的实际大小
	int mCapacity;   //数组的容量
};

//自定义构造

template <class T>
MyArray<T>::MyArray(const int capacity) {
	this->mSize = 0;
	this->mCapacity = capacity;
	this->pAddress = new T[mCapacity];
}

//拷贝构造

template <class T>
MyArray<T>::MyArray(const MyArray & array) {
	mCapacity = array.mCapacity;
	mSize = array.mSize;
	this->pAddress = new T[array.mCapacity];
	for (int index = 0; index < this->mSize; ++index) {
		pAddress[index] = pAddress[index];
	}
}

//获取数组的容量

template <class T>
int MyArray<T>::capacity() const {
	return this->mCapacity;
}

//获取数组当前的元素数

template <class T>
int MyArray<T>::size() const {
	return this->mSize;
}

//尾部插入元素

template <class T>
void MyArray<T>::push(const T &e) {
	if (this->mSize == this->mCapacity) {
		cout << "数组已满,无法插入!!!" << endl;
	}
	else {
	    //注释掉的这一部分是多余处理,其实不需要
		/*
		T *newSpace = new T[mCapacity];
		//for (int i = 0; i < mSize; ++i) {
		//	newSpace[i] = pAddress[i];
		}
		*/
		pAddress[this->mSize] = e;
		this->mSize++;
	}
}

//删除尾部元素

template <class T>
void MyArray<T>::pop() {
	if (!this->mSize) {
		cout << "数组为空,无删除操作" << endl;
		return;
	}
	T *newSpace = new T[mCapacity];
	for (size_t i = 0; i < this->mSize - 1; ++i) {
		newSpace[i] = pAddress[i];
	}
	pAddress = newSpace;
	this->mSize--;
}

//重载访问运算符

template <class T>
T& MyArray<T>::operator[](size_t index) {
	return this->pAddress[index];
}

//自定义析构函数

template <class T>
MyArray<T>::~MyArray() {
	if (this->pAddress)
	    delete[] pAddress;     //堆区空间手动开辟一定记得手动释放
}

最后是函数的测试部分(main函数):(没有技术含量的一部分):

#include <iostream>
#include <string>
#include "myArray.hpp"
using namespace std;

int main()
{
	MyArray<int> array(5);
	cout << "数组的容量是:" << array.capacity() << endl;
	cout << "数组的元素数是:" << array.size() << endl;
	MyArray<int> array2(array);
	cout << "数组的容量是:" << array.capacity() << endl;
	cout << "数组的元素数是:" << array.size() << endl;
	cout << "*************************" << endl;
	cout << "插入元素后的数组:" << endl;
	for (int i = 0; i < 5; ++i) {
		array.push(i);
	}
	cout << "数组的容量是:" << array.capacity() << endl;
	cout << "数组的元素数是:" << array.size() << endl;
	//这里可以使用int和size_t
	for (int i = 0; i < array.size(); ++i) {
		cout << array[i] << " ";
	}
	cout << endl;
	cout << "*************************" << endl;
	cout << "删除元素后的数组:" << endl;
	array.pop();
	cout << "数组的容量是:" << array.capacity() << endl;
	cout << "数组的元素数是:" << array.size() << endl;
	for (int i = 0; i < array.size(); ++i) {
		cout << array[i] << " ";
	}
	cout << endl;
	system("pause");
	return 0;
}

测试结果如下:
在这里插入图片描述

总结:数组的方法还有很多可以自己去尝试实现一下,比如有迭代器、resize等等方法,我觉得只要是知道了使用类模板这一思想,剩下方法的实现都是大同小异。

发布了33 篇原创文章 · 获赞 23 · 访问量 2282

猜你喜欢

转载自blog.csdn.net/weixin_42119041/article/details/102672200
今日推荐