C++通过类模板实现一个可以容下各种对象的万物栈

#include<iostream>
using namespace std;
template<typename T> class Stack
{
public:
	Stack(int size=10):mystack(new T[size]),
		size(size),
		topindex(0)
	{}
	//由于Stack的mystack成员变量的内存在堆上,所以不能使用编译器提供的拷贝构造函数和赋值重载函数,不然会进行浅拷贝
	Stack(const Stack<T>& otherstack)//拷贝构造函数
	{
		size = otherstack.size;
		topindex = otherstack.topindex;
		mystack = new T[size];
		/*
		* 这里不能使用memcpy函数,因为T可能是一个对象类型
		*如果用memcpy函数,就会不断进行浅拷贝构造对象,后面析构就可能出问题
		*/
		for (int i = 0; i < topindex;i++)//
		{
			mystack[i] = otherstack.mystack[i];
		}
	}
	/*
	*返回值类型是Stack而不是void,是因为要满足连续赋值s1=s2=s3
	*引用则是减少了临时变量创建的开销,直接传递地址
	*/
	Stack& operator=(const Stack<T>& otherstack)	//赋值运算符
	{
		if (&otherstack == this)return *this;//防止自赋值,Stack s1;s1=s1;
		//释放调用这个Stack对象本来占用的那块堆空间
		delete[]mystack;
		size = otherstack.size;
		topindex = otherstack.topindex;
		mystack = new T[size];
		for (int i = 0; i < topindex; i++)
		{
			mystack[i] = otherstack.mystack[i];
		}
		return *this;
	}
	~Stack()
	{
		delete[]mystack;
		mystack = nullptr;
	}
	void push(T val)
	{
		if (full())expand();
		mystack[topindex++] = val;
	}
	void pop()
	{
		if (empty())throw "Stack is empty";
		topindex--;
	}
	//对于只进行只读操作的函数一般都要声明为常成员函数,这样一般的对象和常对象都可以调用这个函数
	T top()const//这里不调用empty函数,是因为一般用户使用top函数的时候就会配合empty函数一起使用
	{
		return mystack[topindex - 1];
	}
	bool full()const
	{
		return size == topindex;
	}
	bool empty()const
	{
		return topindex == 0;
	}
private:
	T* mystack;
	int size;
	int topindex;
	void expand()//将这个函数声明为private,是因为扩容是Stack类的内部操作用于自动扩容,用户不需要调用
	{
		T* newstack = new T[size * 2];
		for (int i = 0; i < topindex; i++)
		{
			newstack[i] = mystack[i];
		}
		delete[]mystack;
		mystack = newstack;
	}
};
int main()
{
	Stack<int> s;
	srand(time(NULL));
	for (int i = 0; i < 10; i++)
	{
		s.push(rand() % 100);
	}
	while (!s.empty())
	{
		cout << s.top()<<" ";
		s.pop();
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

咩咩大主教

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值