#include<iostream>
using namespace std;
#include<stack>//stack没有迭代器
#include<string>
/*
stack构造函数
stack<T> stkT;//stack采用模板类实现, stack对象的默认构造形式:
stack(const stack &stk);//拷贝构造函数
stack赋值操作
stack& operator=(const stack &stk);//重载等号操作符
stack数据存取操作
push(elem);//向栈顶添加元素
pop();//从栈顶移除第一个元素
top();//返回栈顶元素
stack大小操作
empty();//判断堆栈是否为空
size();//返回堆栈的大小
*/
void test1()
{
stack<int> s;
s.push(10);
s.push(20);
s.push(30);
s.push(40);
while (!s.empty())
{
cout << s.top() << endl;
s.pop();
}
cout << s.size() << endl;
}
class student
{
public:
student(int Age, string Name)
{
age = Age;
name = Name;
}
public:
int age;
string name;
};
void test2()
{
student s1(10, "aaaa");
student s2(20, "bbbb");
student s3(30, "cccc");
student s4(40, "dddd");
stack<student> s;
s.push(s1);
s.push(s2);
s.push(s3);
s.push(s4);
while (!s.empty())
{
cout <<"Age:"<< s.top().age <<"Name"<< s.top().name << endl;
s.pop();
}
cout << s.size() << endl;
}
int main()
{
test1();
test2();
system("pause");
return 0;
}
打印结果: