MySatck.hpp
模板的声明和实现最好不要分文件编写,会比较麻烦。何在一起写就是.hpp文件
#pragma once
#include <iostream>
using namespace std;
template<class T>
class MyStack
{
public:
//构造函数
MyStack(int capacity) {
this->m_Capacity = capacity;
this->m_Size = 0;
pAddress = new T[this->m_Capacity];
}
//拷贝构造函数
MyStack(const MyStack& stack_Arr) {
this->m_Capacity = stack_Arr.m_Capacity;
this->m_Size = stack_Arr.m_Size;
this->pAddress = new T[this->Capacity];
for (int i = 0; i < this->m_Size; i++) {
//如果T为对象,而且还包含指针,必须需要重载 = 操作符,因为这个等号不是 构造 而是赋值,
// 普通类型可以直接= 但是指针类型需要深拷贝
this->pAddress[i] = stack_Arr.pAddress[i];
}
}
//重载= 防止浅拷贝的问题
MyStack& operator= (const MyStack& myStack) {
if (this->pAddress != NULL) {
delete[] this->pAddress;
this->m_Capacity = 0;
this->m_Size = 0;
}
this->m_Capacity = myStack.m_Capacity;
this->m_Size = myStack.m_Size;
this->pAddress = new T[this->m_Capacity];
for (int i = 0; i < this->m_Size; i++) {
this->pAddress[i] = myStack[i];
}
return *this;
}
//重载[] 操作符 arr[0]
T& operator [](int index)
{
return this->pAddress[index]; //不考虑越界,用户自己去处理
}
//插入
void Push_back(const T& val)
{
if (this->m_Capacity == this->m_Size)
{
//int newC = this->getCapacity * 2;
//MyStack<T> newStack(&newC);
//newStack.m_Size = this->m_Size;
//for (int i = 0; i < this->m_Size; i++) {
// newStack[i] = this->pAddress[i];
//}
//T* temp = this->pAddress;
//this->pAddress = newStack.pAddress;
//if (temp!= NULL) {
// delete[] temp;
//}
return;
}
this->pAddress[this->m_Size] = val;
this->m_Size++;
}
//删除
void Pop_back()
{
if (this->m_Size == 0)
{
return;
}
this->m_Size--;
}
T& top() {
return this->pAddress[getSize() - 1];
}
//获取容量
int getCapacity()
{
return this->m_Capacity;
}
//获取大小
int getSize()
{
return this->m_Size;
}
//析构
~MyStack()
{
if (this->pAddress != NULL)
{
delete[] this->pAddress;
this->pAddress = NULL;
this->m_Capacity = 0;
this->m_Size = 0;
}
}
private:
T* pAddress;//指向一个堆空间,真正的数据
int m_Capacity;//容量
int m_Size;//大小
};
testMyStack.cpp
#include "Mystack.hpp"
#include <string>
using namespace std;
template <class T >
void printMyStack(MyStack<T>& stack_Arr) {
for (int i = 0; i < stack_Arr.getSize(); i++) {
cout << stack_Arr[i] << " ";
}
cout << endl;
}
//test
void test01() {
MyStack<int> stack1(10);
for (int i = 0; i < 10; i++)
{
stack1.Push_back(i);
}
cout << "stack1: " << endl;
printMyStack(stack1);
cout << "stack1 capacity is " << stack1.getCapacity()<< endl;
cout << "stack1 size is " << stack1.getSize()<< endl;
cout << "stack top is " << stack1.top() << endl;
}
//测试自定义数据类型
class Person {
public:
Person() {}
Person(string name, int age) {
this->m_Name = name;
this->m_Age = age;
}
public:
string m_Name;
int m_Age;
};
//普通函数和模板函数同名 会自动匹配最优
void printMyStack(MyStack<Person>& myStack)
{
for (int i = 0; i < myStack.getSize(); i++) {
cout << "姓名:" << myStack[i].m_Name << " 年龄: " << myStack[i].m_Age << endl;
}
}
void test02()
{
//创建
MyStack<Person> pStack(10);
Person p1("孙悟空", 30);
Person p2("韩信", 20);
Person p3("妲己", 18);
Person p4("王昭君", 15);
Person p5("赵云", 24);
//插入数据
pStack.Push_back(p1);
pStack.Push_back(p2);
pStack.Push_back(p3);
pStack.Push_back(p4);
pStack.Push_back(p5);
printMyStack(pStack);
cout << "pStack is size:" << pStack.getSize() << endl;
cout << "pStack is capacity:" << pStack.getCapacity() << endl;
}
int main() {
test02();
system("pause");
}
运行结果: