引言:雖然數組不是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;
}
測試結果如下: