1. 程式人生 > >stack容器基本操作

stack容器基本操作

#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; }