1 pop();

出栈

2 push();

入栈

3 size();

返回栈中元素个数

4 top();

返回栈顶元素

 

使用栈,把十进制转换为二进制

 

 1 #include <iostream>
 2 #include <stack>
 3 
 4 int main()
 5 {
 6     int num;
 7     std::stack<int>mystack;
 8 
 9     std::cin >> num;
10 
11     for (; num; num /= 2)
12     {
13         mystack.push(num % 2);//入栈
14         std::cout << "当前元素个数" << mystack.size() << std::endl;
15     }
16 
17     while (!mystack.empty())
18     {
19         std::cout << mystack.top() << " ";//返回栈顶元素
20         mystack.pop();//出栈
21     }
22 
23     return 0;
24 }

 

相关文章:

  • 2021-06-03
  • 2022-12-23
  • 2022-01-30
  • 2021-12-24
  • 2021-07-12
  • 2021-09-16
猜你喜欢
  • 2022-12-23
  • 2021-07-27
  • 2022-12-23
  • 2021-08-14
  • 2022-03-04
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案