0
点赞
收藏
分享

微信扫一扫

STL 栈 结构体 C++

山竹山竹px 2022-04-14 阅读 58
c++stack
#include <iostream>
using namespace std;
#include <cstdio>
#include <stack>

//状态类
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;
}

在这里插入图片描述

举报

相关推荐

0 条评论