std::stack

  1. 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

  1. #include <iostream>
  2. #include <stack>
  3. #include <vector>
  4. #include <deque>
  5. using namespace std;
  6. int main(int argc, char **argv)
  7. {
  8. deque<int> mydeque(3,10);
  9. vector<int> myvector(2,20);
  10. stack<int> first;
  11. stack<int> second(mydeque);
  12. stack<int, vector<int> > third;
  13. stack<int, vector<int> > fourth(myvector);
  14. /** other function please to see the deque container */
  15. return 0;
  16. }

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. 基于Asterisk的VoIP开发指南——(1)实现基本呼叫功能

    原文:基于Asterisk的VoIP开发指南--(1)实现基本呼叫功能 说明: 1.本文档探讨基于Asterisk如何实现VoIP的一些基本功能,包括基本呼叫功能的方案选取.主叫号码透传.如何编写As ...

  2. 【Linux 工作经常使用命令 】

    1, 批量杀某个程序 比方某个程序叫  url_info.py, 起了若干个进程 . 高速查杀. 先查看 ps aux | grep url_info.py 确认没问题 ,能够杀,则批量kill ps ...

  3. ASP.NET vNext 在 Mac OS

    让 ASP.NET vNext 在 Mac OS 中飞呀飞...   写在前面 阅读目录: 娓娓道来 Install ASP.NET vNext Command Line Tools 安装 Homeb ...

  4. [转]Windows下搭建PHP开发环境

    原文:http://www.cnblogs.com/pharen/archive/2012/02/06/2340628.html PHP集成开发环境有很多,如XAMPP.AppServ......只要 ...

  5. touch命令功能

    touch命令功能   touch fileA,如果fileA存在,使用touch命令可更改这个文件或目录的日期时间,包括存取时间和更改时间:如果fileA不存在,touch命令会在当前目录下新建一个 ...

  6. Unit Of Work-工作单元

    Unit Of Work-工作单元 阅读目录: 概念中的理解 代码中的实现 后记 掀起了你的盖头来,让我看你的眼睛,你的眼睛明又亮呀,好像那水波一模样:掀起了你的盖头来,让我看你的脸儿,看看你的脸儿红 ...

  7. Windows Live Writer 完成开源并推出开源分支

    原文:Announcing Open Live Writer - An Open Source Fork of Windows Live Writer Windows Live Writer是一款发布 ...

  8. C#程序的157个建议

    编写高质量代码改善C#程序的157个建议——导航开篇   前言 由于最近工作重心的转移,原来和几个同事一起开发的项目也已经上线了,而新项目就是在现有的项目基础上进行优化延伸扩展.打个比方,现在已经上线 ...

  9. SQLServer数据库误删数据找回

    记一次SQLServer数据库误删数据找回 昨天 同事在本机清理数据库表时,连接到了生产机,误删了二十几张表,幸好是晚上加班的时候删除的,生产机上当时是一天一备份,还原备份是最后的策略,最关键的还是要 ...

  10. 建立Ftp站点

    建立Ftp站点步骤: 1.首先创建一个用户 我的电脑右键->管理->本地用户和组->用户->“右键”新建用户->输入用户名和密码再点创建就行了! 2.其次是在C盘新建文件 ...