包含min的栈

#include <iostream>
#include <stack>
using namespace std;

void push(stack<int> &s1,stack<int> &s2,int value)
{
    s1.push(value);
    if(s2.size()<=0||value<s2.top())
        s2.push(value);
    else
        s2.push(s2.top());
    //cout<<s2.top();
}

void pop(stack<int> &s1,stack<int> &s2)
{
    s1.pop();
    s2.pop();
}

int main()
{
    int data[]={3,4,2,1};
    stack<int> s1,s2;
    for(int i=0;i<4;i++)
    {
        push(s1,s2,data[i]);
        cout<<s2.top();
    }
    return 0;
}

也可以使用类

原文地址:https://www.cnblogs.com/home123/p/7446726.html