stack容器基本操作

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Duke10/article/details/78348696
#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;
}
展开阅读全文

没有更多推荐了,返回首页