STL_stack

简介: STL_stack

STL_stack

简介:本文通过一段代码,教大家学会stack的使用。

stack容器基本概念

stack常用接口

学习代码:

#include<iostream>
#include<stack>
using namespace std;
// Stack container
void test01()
{
  // Features: in line with the first in and out
  // 栈的特性:栈是一种后进先出的数据结构
  stack<int> s;  // default contruction
  // push
  // 往栈里面放入元素
  s.push(10);
  s.push(20);
  s.push(30);
  s.push(40);
  cout << "栈的大小:" << s.size() << endl;
  // As long as the stack is not empty, check the stack and  execute the stack operation
  while (!s.empty())
  {
    // View top of stack elements
    // 查看栈顶元素
    cout << "栈顶元素为:" << s.top() << endl;
    // pop
    // 出栈
    s.pop();
  }
  cout << "栈的大小:" << s.size() << endl;
}
int main()
{
  test01();
  return 0;
}

运行结果:

相关文章
|
22天前
|
C++ 容器
【C++初阶】STL详解(六)Stack与Queue的介绍与使用
【C++初阶】STL详解(六)Stack与Queue的介绍与使用
19 1
|
22天前
|
存储 C++ 容器
【C++初阶】STL详解(七)Stack与Queue的模拟实现
【C++初阶】STL详解(七)Stack与Queue的模拟实现
13 1
|
2月前
|
编译器 C++ 容器
STL常用之vector,list,stack,queue,deque总结与对比
STL常用之vector,list,stack,queue,deque总结与对比
|
3月前
|
存储 C++ 容器
STL--stack、queue实现
STL--stack、queue实现
|
9月前
|
存储 设计模式 C++
C++ STL stack & queue
stack是一种容器适配器,专门用在具有后进先出操作的上下文环境中,其删除只能从容器的一端进行元素的插入与提取操作。
|
4月前
|
算法 容器
C++13-STL模板-栈stack
C++13-STL模板-栈stack
|
5月前
|
C++ 容器
STL中stack和queue的使用以及模拟实现
STL中stack和queue的使用以及模拟实现
39 0
|
6月前
|
容器
STL-stack
STL-stack
30 0
|
10月前
|
C++ 容器
【C++ STL】 --- stack
【C++ STL】 --- stack
52 0
|
11月前
|
设计模式 C++ 容器
C++【STL】之stack和queue学习
C++ STL stack和queue常用接口和模拟实现详细讲解,干货满满!
83 0
C++【STL】之stack和queue学习