STL的栈(C++)
#include<iostream>
#include<stack>
using namespace std;
//将十进制的数据n转换成m进制的数据
//stack是STL的
stack<int> conversion(unsigned int n,unsigned int m)//unsigned 是无符号整型
{
stack<int> s;
while(n)
{
s.push(n%m);
n = n/m;
}
return s;
}
int main()
{
int n = 1348;
//将n转换成8进制
stack<int> s = conversion(n,8);
while(!s.empty())
{
cout<<s.top();
s.pop();
}
cout<<endl;
//将n转换成2进制
s = conversion(n,2);
while(!s.empty())
{
cout<<s.top();
s.pop();
}
cout<<endl;
}