STL 栈存储类 C++

STL 栈存储类 C++,第1张

#include 
using namespace std;
#include 
#include 

//状态类
class State
{
private:
	int x;
	int y;
	int z;

public:
	State(int a, int b, int c)		//定义构造函数
	{
		x = a;
		y = b;
		z = c;
	}

	void print()					//定义打印方法
	{
		printf("%d %d %d\n", x, y, z);
	}
};

stack<State> path;

int main()
{
	State s1 = State(0, 1, 2);
	State s2 = State(3, 4, 5);
	State s3 = State(2, 2, 2);
	State s4 = State(0, 1, 3);
	
	//测试数据入栈
	path.push(s1);
	path.push(s2);
	path.push(s3);
	path.push(s4);
	printf("path的大小:%d\n", path.size());

	//遍历出栈
	while (path.empty() == 0)
	{
		path.top().print();
		path.pop();
	}
	return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/662121.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-18
下一篇 2022-04-18

发表评论

登录后才能评论

评论列表(0条)

保存