stack容器基本操作

来源:互联网 发布:华为网络机顶盒哪款好 编辑:程序博客网 时间:2024/05/21 22:35
#include<iostream>using namespace std;#include"stack"void f1(){    stack<int> s1;    //入栈    for(int i = 0; i < 10; i++)    {        s1.push(i + 1);    }    cout << "栈的大小为: " << s1.size() << endl;    //出栈    while (!s1.empty())    {        int temp = s1.top(); //获取栈顶元素        cout << temp << " ";        s1.pop();        //弹出栈顶元素    }}//定义一个人结点class Person{public:    int age;    char name[20];public:    void prinT()    {        cout << "age: " << age << endl;    }};void f2(){    Person p1, p2, p3;    p1.age = 11;    p2.age = 12;    p3.age = 13;    stack<Person> p;    p.push(p1);    p.push(p2);    p.push(p3);    while(!p.empty())    {        Person temp = p.top();        temp.prinT();        p.pop();    }}int main(){    //f1();    f2();    return 0;}