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 ...
随机推荐
- - C#编程大幅提高OUTLOOK的邮件搜索能力!
原文:[原创] - C#编程大幅提高OUTLOOK的邮件搜索能力! 使用OUTLOOK, 你有没有遇到过上图的问题? 多达18419封邮件! 太多了, 每次想找一个邮件都非常耗时, 想办法解决这个问题 ...
- 用批处理编译*.sln工程
原文:用批处理编译*.sln工程 批处理是直接调用Microsoft Visual Studio 8\Common7\IDE\ 目录内的 devenv.exe ,它启动后就是IDE,提供的参数如下: ...
- [译]Java 设计模式之抽象工厂
(文章翻译自Java Design Pattern: Abstract Factory) 抽象工厂模式针对工厂模式增加了抽象层.如果我们使用抽象工厂模式和工厂模式比较的话,很明显抽象工厂模式增加了一个 ...
- RPL协议介绍
RPL是IPv6 Routing Protocol for Low-Power and Lossy Networks的简称. 低功耗及有损网络(LLN)是一类内部链接和路由器都受限的网络,该网络下的路 ...
- Solr 教程
1.Solr安装 下载jdk-8u111-windows-i586_8.0.1110.14 下载solr-6.3.0.zip 2.配置JAVA_HOME 在"系统变量"中,设置3项 ...
- Mysql高级之索引
原文:Mysql高级之索引 索引:是针对数据所建立的目录. 作用: 可以加快查询速度 负面影响: 降低了增删改的速度. 索引的创建原则: 1:不要过度索引 2:在where条件最频繁的列上加.在重复度 ...
- cocos2d-x3.2中将XCode发展project转移到VS2010可能会发生错误
一些代码在线xcode写.我们希望我们自己的屌丝vs上述的实施,要重新构建它project,然后加载.但是绝对 没想到在VS里新建project再加入文件,编译后出现了好多错误.以下就把解决这些错误的 ...
- 配置serv-u access数据库遇到的一些问题
配置好access数据库后,需要建个web页面来供用户修改密码,但修改时,提示:odbc被占用无法打开. serv-u一直在保持打开access数据库.我们需要将数据库设置默认打开方式为共享,不锁定. ...
- Javascript中布尔运算符的高级应用
对象检测语句:var W3CDOM = document.getElementsByTagName && document.createElement 为了执行运算符&& ...
- 仿async/await(一)and Gulp:新一代前端构建利器
NET 4.5的async/await真是个神奇的东西,巧妙异常以致我不禁对其实现充满好奇,但一直难以窥探其门径.不意间读了此篇强文<Asynchronous Programming in C# ...