下面的程序使用stack实现了数制间的转换:
#include<iostream>
#include<stack>
using namespace std;
int main(){
stack<int> st;
int n;
cin>>n;
while(n){
st.push(n%2);
n/=2;
}
while(!st.empty()){
cout<<st.top();
st.pop();
}
cout<<endl;
return 0;
}
//empty() 堆栈为空则返回真
//pop() 移除栈顶元素
//push() 在栈顶增加元素
//size() 返回栈中元素数目
//top() 返回栈顶元素
转载于:https://blog.51cto.com/beyond316/1256060