std::stack

template <class T, class Container = deque<T> > class stack;

LIFO stack

Stacks are a type of container adaptor, specifically designed to operate in a LIFO context (last-in first-out), where elements are inserted and extracted only from one end of the container.

stacks are implemented as containers adaptors, which are classes that use an encapsulated object of a specific container class as its underlying container, providing a specific set of member functions to access its elements. Elements are pushed/popped from the "back" of the specific container, which is known as the top of the stack.

The underlying container may be any of the standard container class templates or some other specifically designed container class. The container shall support the following operations:

  • empty
  • size
  • back
  • push_back
  • pop_back

The standard container classes vector, deque and list fulfill these requirements. By default, if no container class is specified for a particular stack class instantiation, the standard container deque is used.

Template parameters

  • T Type of the elements. Aliased as member type stack::value_type.
  • Container Type of the internal underlying container object where the elements are stored. Its value_type shall be T. Aliased as member type stack::container_type.
member type definition notes
value_type The first template parameter (T) Type of the elements
container_type The second template parameter (Container) Type of the underlying container
size_type an unsigned integral type usually the same as size_t

Member functions

  • (constructor) Construct stack (public member function )
  • empty Test whether container is empty (public member function )
  • size Return size (public member function )
  • top Access next element (public member function )
  • push Insert element (public member function )
  • emplace Construct and insert element (public member function )
  • pop Remove top element (public member function )
  • swap Swap contents (public member function )

Non-member function overloads

  • relational operators Relational operators for stack (function )
  • swap (stack) Exchange contents of stacks (public member function )

Non-member class specializations

  • uses__allocator Uses allocator for stack (class template )

Code Example

#include <iostream>
#include <stack>
#include <vector>
#include <deque> using namespace std; int main(int argc, char **argv)
{
deque<int> mydeque(3,10);
vector<int> myvector(2,20); stack<int> first;
stack<int> second(mydeque); stack<int, vector<int> > third;
stack<int, vector<int> > fourth(myvector); /** other function please to see the deque container */
return 0;
}

Reference

cplusplus


C++ std::stack的更多相关文章

  1. 从deque到std::stack,std::queue,再到iOS 中NSArray(CFArray)

    从deque到std::stack,std::queue,再到iOS 中NSArray(CFArray) deque deque双端队列,分段连续空间数据结构,由中控的map(与其说map,不如说是数 ...

  2. why std::stack has separate top() and pop()

    SGI explanation: http://www.sgi.com/tech/stl/stack.html One might wonder why pop() returns void, ins ...

  3. C++ std::stack 基本用法

    #include <iostream> #include <string> #include <stack> // https://zh.cppreference. ...

  4. 容器适配器之stack

    参见http://www.cplusplus.com/reference/stack/stack/ template<class T, class Container = deque<T& ...

  5. #include <stack>

    1 pop(); 出栈 2 push(); 入栈 3 size(); 返回栈中元素个数 4 top(); 返回栈顶元素 使用栈,把十进制转换为二进制 #include <iostream> ...

  6. C++标准库之stack(各函数及其使用全)

    原创作品,转载请注明出处:http://www.cnblogs.com/shrimp-can/p/5283207.html 栈是后入先出的.成员函数有: 1.栈的声明 std::deque<in ...

  7. STL之容器适配器stack的实现框架

    说明:本文仅供学习交流,转载请标明出处,欢迎转载! 一提到适配器(adapter).我们就想到了早期用电话线上网所用的调制解调器,俗称"猫"."猫"的作用是实现 ...

  8. 温故而知新----stack

    stack:栈,先进后出,操作方法相对其它容器来说比较少,具有以下特性:1.LIFO 后进先出,与队列相反,队列时FIFO(先进先出)2.没有迭代器访问.3.C++ 11标准中新增了两个接口,如下:  ...

  9. c++stack类的用法

    官方解释: LIFO stack Stacks are a type of container adaptor, specifically designed to operate in a LIFO ...

随机推荐

  1. android的EditText获取另一个焦点

    在android关于开发过程EditText在setFocusable(false);后,设置时需要再次获得输入焦点setFocusable(true);问题后仍然无法获得焦点: 解决的方法: 对Ed ...

  2. Machine Learning - XI. Machine Learning System Design机器学习系统的设计(Week 6)

    http://blog.csdn.net/pipisorry/article/details/44119187 机器学习Machine Learning - Andrew NG courses学习笔记 ...

  3. JAVA开发语言基础

    很多时候我们都不知道,为什么android最开始要用java作为基础语言 看看知乎上都是怎么回答的 为什么java最初成为android开发的基础语言 ------------------------ ...

  4. linux sar 命令详解(转载)

    linux sar 命令详解 2013-04-01 11:05 [小 大] 来源: 开源中国社区 评论: 0 分享至: 百度权重查询 词库网 网站监控 服务器监控 SEO监控 手机游戏 iPhone游 ...

  5. RequireJS 入门指南

    RequireJS 入门指南 http://requirejs.org/ 简介如今最常用的JavaScript库之一是RequireJS.最近我参与的每个项目,都用到了RequireJS,或者是我向它 ...

  6. Ubuntu下安装Intel Fortran编译器(ifort)

    Intel Fortan Compiler简称ifort, Windows下的ifort是收费的,但是Linux系统下提供免费的ifort,可以在下面的链接中下载需要的版本(必须先注册,随后会收到官网 ...

  7. 在线web编辑器

    真正在线编辑的在线web编辑器 最近正在研究开发一款在线web编辑器架构,这是一款真正傻瓜式的web编辑器,可以在正常浏览页面的情况进行编辑,经过测试,对于一般网页页面来说非常好用方便,操作更简单. ...

  8. 简化MonoTouch.Dialog的使用

    读了一位园友写的使用MonoTouch.Dialog简化iOS界面开发,我来做个补充: 相信使用过DialogViewController(以下简称DVC)的同学都知道它的强大,但是缺点也是明显的,应 ...

  9. 2014.3.6-C语言学习小结

    链表基础: 知识点: 1.链表基础 2.节点的创建和添加 llist_append_node 3.链表的遍历 llist_print_each 4.链表的查找与修改 5.链表的插入与删除 6.链表的销 ...

  10. EditPlus 3设置字体大小

    EditPlus设置字体大小 tools ---> preferences ---> fonts