#include <iostream>
#include <stack>
using namespace std;
//传入栈时一定要传入地址,这样才是引用.
void sortStackByStack(stack<int>& st) {
stack<int> help;
while (!st.empty()) {
int cur = st.top();
st.pop();
//先从小到大排序.
while (!help.empty() && cur>help.top()) {
st.push(help.top());
help.pop();
}
help.push(cur);
}
//再让st从大到小排序.
while (!help.empty()) {
st.push(help.top());
help.pop();
}
}
int main() {
stack<int> st;
st.push(3); st.push(2); st.push(1);
sortStackByStack(st);
cout << st.top() << endl;
cin.get();
return 0;
}