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. 打印man手册为pdf文件

    只需要一个命令就可以了! merlin@tfAnalysis:~/projects/tfadc$ man -t errno | ps2pdf - ~/errno.pdf 输出的文件很漂亮.

  2. 通过localstorage和cookie实现记录文章的功能

    我们在做页面的时候,会考虑记录用户曾经看过的文章的功能,并记录下来在页面中显示!但是在IE低版本的下是不支持localstorage的功能,只能采用cookie来代替本地存储的功能!实现的方法如下! ...

  3. .Net中批量更新或添加数据

    方法一:使用SqlBulkCopy实现批量更新或添加数据. SqlBulkCopy类一般只能用来将数据批量插入打数据库中,如果数据表中设置了主键,出现重复数据的话会报错,如果没有设置主键,那么将会添加 ...

  4. 谈谈Oracle dba_free_space

    谈谈Oracle dba_free_space 博客分类: ORACLE管理 OracleSQLC#C++C  顾名思义,dba_free_space指的是Oracle还有多少表空间剩余空间,其视图结 ...

  5. Lambda表达式、依赖倒置

    ASP.NET MVC学前篇之Lambda表达式.依赖倒置 ASP.NET MVC学前篇之Lambda表达式.依赖倒置 前言 随着上篇文章的阅读,可能有的朋友会有疑问,比如(A.Method(xxx= ...

  6. JavaScript原生数组函数

    有趣的JavaScript原生数组函数 在JavaScript中,可以通过两种方式创建数组,构造函数和数组直接量, 其中后者为首选方法.数组对象继承自Object.prototype,对数组执行typ ...

  7. iOS基础 - Copy

    copy和mutableCopy 一个对象使用copy或mutableCopy方法可以创建对象的副本 copy – 需要先实现NSCoppying协议,创建的是不可变副本(如NSString.NSAr ...

  8. 重温HTML的基础

    新年新气象,新的一年要有自己的学习计划与工作计划.希望大家能够共享. 经过一段时间,我想重新复习与学习一下HTML的基础,我呢打算整理一下W3C里面的知识,也许对某些人没有任何作用,但是对我来说这是我 ...

  9. JS 浏览器检测

    原创博文,转载请注明出处. 如果你现在还是一个JavaScript新手(比如本人,哈哈 ),由于浏览器的兼容问题,很容易写出不兼容的JS代码,这就需要我们对浏览器进行检测. JavaScript Na ...

  10. python实现变参

    使用赋值表达式传递参数,可以颠倒参数列表的顺序.函数的参数提供默认参数 参数可以是变量,也可以是元祖.列表等内置的数据结构 在程序开发中,常常需要传递可变长度的参数.在函数的参数前使用标识符“*”可以 ...