C++ std::stack
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
C++ std::stack的更多相关文章
- 从deque到std::stack,std::queue,再到iOS 中NSArray(CFArray)
从deque到std::stack,std::queue,再到iOS 中NSArray(CFArray) deque deque双端队列,分段连续空间数据结构,由中控的map(与其说map,不如说是数 ...
- 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 ...
- C++ std::stack 基本用法
#include <iostream> #include <string> #include <stack> // https://zh.cppreference. ...
- 容器适配器之stack
参见http://www.cplusplus.com/reference/stack/stack/ template<class T, class Container = deque<T& ...
- #include <stack>
1 pop(); 出栈 2 push(); 入栈 3 size(); 返回栈中元素个数 4 top(); 返回栈顶元素 使用栈,把十进制转换为二进制 #include <iostream> ...
- C++标准库之stack(各函数及其使用全)
原创作品,转载请注明出处:http://www.cnblogs.com/shrimp-can/p/5283207.html 栈是后入先出的.成员函数有: 1.栈的声明 std::deque<in ...
- STL之容器适配器stack的实现框架
说明:本文仅供学习交流,转载请标明出处,欢迎转载! 一提到适配器(adapter).我们就想到了早期用电话线上网所用的调制解调器,俗称"猫"."猫"的作用是实现 ...
- 温故而知新----stack
stack:栈,先进后出,操作方法相对其它容器来说比较少,具有以下特性:1.LIFO 后进先出,与队列相反,队列时FIFO(先进先出)2.没有迭代器访问.3.C++ 11标准中新增了两个接口,如下: ...
- c++stack类的用法
官方解释: LIFO stack Stacks are a type of container adaptor, specifically designed to operate in a LIFO ...
随机推荐
- Web开发的发展
领导以前是做C的,没有做过Web开发,就问我,Web技术发展的大致过程,我就是简单的说了开发过程的演化,下来后有自己找些资料补充下,如下所示:(着这是个简单的说明,感兴趣的可以再自己找找资料). 1. ...
- 个人实现的一个简单的蜗牛矩阵(c语言)
#include<stdio.h> #include<stdlib.h> int main(void) { int n,m; int x,y; int **array; int ...
- javascript 函数介绍
javascript函数使用的时候,往往都比较单一,这里介绍几种不同于我们之前使用的函数调用方式! 1.函数表达式包含名称,用于递归 var f = function s(num) { if (num ...
- 算法回顾--N皇后问题简单回顾
前言 最近学习的过程中,不知道哪门子的思维发散,突然又遇见皇后问题了,于是乎老调重弹,心里琢磨,虽然思路大家都容易懂,哪怕是最简单的野蛮回溯法,说着简单,但是如果非得编码实现?我可以一次性写出来OK的 ...
- 逐步在Windows上结合CopSSH + msysGit安装安装Git Server同时集成Git使用Visual Studio
v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VM ...
- 凝结时光:ImageMagick 制作 gif
从去年12.12开始,工作日都会从单位的落地窗向外拍一张照片,有点每日打卡的意思.weekday 是一天不落的,weekend 不是每次都到单位,落了几次. 想法来自上学的时候,有同学在同一个地方,拍 ...
- 线程池python
原创博文,转载请注明出处 今天在学习python进程与线程时,无意间发现了线程池threadpool模块,见官方文档. 模块使用非常简单,前提是得需要熟悉线程池的工作原理. 我们知道系统处理任务时,需 ...
- 微信JS-SDK分享接口示例中jsapi_ticket.php和access_token.php说明
关于如何使用微信JS-SDK,可以查看官方介绍说明:查看链接 在使用分享接口的过程中,查看了示例代码,其中包含两个文件jsapi_ticket.php和access_token.php,记录一下这两个 ...
- Code First 启用迁移时出错 HRESULT:0x80131040
问题:Enable-Migrations 使用“8”个参数调用“CreateInstanceFrom”时发生异常 (异常来自 HRESULT:0x80131040) PM> Enable-Mig ...
- 搞定KMP匹配算法
KMP算法介绍及实现——轻松搞定KMP匹配算法 本文介绍了字符串匹配算法中的BF算法和KMP算法.本文中KMP算法介绍部分是关于KMP算法相关文章中最简洁的一篇文章之一.下一篇将继续介绍Horspoo ...