C++的STL之stack

来源:互联网 发布:revit mep软件下载 编辑:程序博客网 时间:2024/06/06 03:31
#include <iostream>#include <stack>using namespace std;int main1(void)            //十进制转换二进制{stack<int>Mystack;int num;cin >> num;while (num)               {int data = num % 2;Mystack.push(data);            //存储num = num / 2;}while (!Mystack.empty())     //不为空{int k = Mystack.top();    //顶部的数值cout << k << " ";           //输出Mystack.pop();              //在栈的顶部移除元素}cin.get();cin.get();return 0;}int main(void){int a[10] = { 1,2,3,4,5,6,7,8,9,10 };stack<int>Mystack;for (int i = 0; i < 10; i++){Mystack.push(a[i]);     //入栈}while (!Mystack.empty()){int k = Mystack.top();cout << k << "  ";        //输出Mystack.pop();}cin.get();cin.get();return 0;}