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 ...
随机推荐
- Android 4.4堆叠结构的变化
我们知道,activity 在 AMS 的形式是 ActivityRecord,task 在 AMS 的形式TaskRecord,流程 AMS 该管理形式 ProcessRecord. 我们先看下 4 ...
- Visual Studio 单元测试之五---数据库测试
原文:Visual Studio 单元测试之五---数据库测试 数据库的单元测试主要是测试数据库中的数据是否符合特定的条件,Visual Studio 2010支持下面几种数据的单元测试类型(Visu ...
- C# 以嵌入到窗体的方式打开外部exe
using System; using System.Collections.Generic; using System.Text; using System.Diagnostics; using S ...
- 工欲善其事:编辑器之神Vim(一)
本篇文章收集了Vim最常用和实用的一些命令,掌握这些命令就可以使用Vim了 本文仅列举个人认为常用的命令,本系列文章更像是自己的学习笔记,而不是VIM使用教程. 如果你想了解vim的更多,可以看看Co ...
- 【转】android 欢迎界面翻页成效,仿微信第一次登陆介绍翻页界面
android 欢迎界面翻页效果,仿微信第一次登陆介绍翻页界面 本实例做的相对比较简单主要是对翻页控件的使用,有时候想要做一些功能是主要是先了解下是否有现成的控件可以使用,做起来比较简单不用费太大的劲 ...
- NServiceBus 入门2
NServiceBus官方文档翻译(二)NServiceBus 入门 在这篇教程中我们将学习如何创建一个非常简单的由客户端向服务端发送消息的订单系统.该系统包括三个项目:Client.Server ...
- ext日期加减任意天数
1.Ext.util.Format.date(new Date().add(Date.DAY, 5), 'Y-m-d'), 'Y-m-d') 2.Ext.util.Format.date(new Da ...
- 开源Asp.Net MVC网上商城BrnShop
开源Asp.Net MVC网上商城BrnShop正式发布,提供源码下载 BrnShop网上商城是以Asp.Net mvc3为基础开发的网上商城,源代码完全开源(企业版的源代码目前还没有完全整理完成,一 ...
- ArcGIS 10.1 发布使用ArcEngine自定义的GP服务
1. 新建立GP模型 在VS2010中新建一个普通的程序及,引入ArcEngine相关的dll.在该DLL中定义一个或多个GP类和一个GP工厂类.GP类要继承IGPFunction2接口,GP工厂类要 ...
- 附加被分离DB
如何附加被分离的质疑数据库? 简介 有些时间,由于日志损坏等原因,导致了数据库质疑.如果此时你分离了数据库,那你会发现你无法再附加上数据库,那后果还是很严重的.因此本文提供了一种方式,可以使得当数 ...