正版STL栈的基本操作

#include <bits/stdc++.h>

using namespace std;

struct Node {
	string x, y, z;
	Node (string x, string y, string z) : x(x), y(y), z(z) {};
};

stack<Node> st; //结构体栈 
stack<string> s;

int main () {
	
	s.push ("ULTRAMAN");
	s.push ("DOGE");
	
	cout << "empty?: " << s.empty() << endl; //不是空的,输出0 
	cout << s.size() << endl;
	cout << s.top() << endl;
	s.pop();
	cout << s.top() << endl;
	s.pop();
	cout << "empty?: " << s.empty() << endl; //输出1 
	
	s.push ("CARD");
	s.push ("DOGE");
	
	while (!s.empty()) s.pop();
	
	cout << "empty?: " << s.empty();
	
//	---------普通栈操作↑----------
//	---------结构体栈↓----------
	
	st.push (Node ("ORB", "GEED", "ZERO"));
	st.push (Node ("GAYA", "X", "DANA"));
	
	cout << st.size() << endl;
	cout << st.top().x << " " << st.top().y << " " << st.top().z << endl;
	st.pop();
	cout << st.top().x << " " << st.top().y << " " << st.top().z;
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Runcode8/article/details/131888742