Stack (stl)

Stephen_X_x發表於2019-06-21
#include<cstdio>
#include<stack>
#include<algorithm>
#include<iostream>
using namespace std;
/////////////////////////// 
typedef int type;
///////////////////////////

///////////////////////////
int main()
{
    stack<type> s1;
    if(s1.empty())
        cout<<"stack is empty now;"<<endl;
//  stack<int>::iterator it = s1.top();//stack and queue dosen't have iterator
    for(int i=1;i<=5;i++)
        s1.push(i);
    cout<<"the size of s1 is "<<s1.size()<<endl;
    while(!s1.empty())
    {
        cout<<s1.top()<<" ";
        s1.pop();
    }
    for(int i=1;i<=5;i++)
        s1.push(i);
    cout<<endl;
    stack <type> s2;
    for(int i=11;i<=20;i++)
        s2.push(i);
    swap(s1,s2);
    cout<<"s1:";
    while(!s1.empty())
    {
        cout<<s1.top()<<" ";
        s1.pop();
    }
    cout<<endl<<"s2:";
    while(!s2.empty())
    {
        cout<<s2.top()<<" ";
        s2.pop();
    }
    cout<<endl;

}
本作品採用《CC 協議》,轉載必須註明作者和本文連結