容器适配器之stack
参见http://www.cplusplus.com/reference/stack/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.
[堆栈(stack)是一种容器适配器,具有后进先出的结构元素的插入和提取都只在容器的一端进行]
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.
[标准容器类vector、deque和list都满足这些要求。默认的内在容器是deque]
/*
//construct stack
stack (const container_type& ctnr = container_type()); ctnr
Container object.
[ctnr是一个容器类对象]
container_type is the type of the underlying container type (defined as an alias of the second class template parameter, Container; see member types).
[ctnr的数据类型container_type是内在容器的数据类型,即第二个模板参数Container] A container adaptor keeps internally a container object as data. This container object is a copy of the ctnr argument passed to the constructor, if any, otherwise it is an empty container.
[stack会将一个容器对象作为数据,这个容器对象是传递到构造函数中的参数ctnr,如果没有传递参数ctnr则为空容器]
*/
#include <iostream>
#include <stack>
#include <vector>
#include <deque> int main()
{
std::deque<int> mydeque(, );
std::vector<int> myvector(, ); std::stack<int> first; // empty stack with deque as underlying container
std::stack<int> second(mydeque); // stack initialized to copy of deque with deque as underlying container std::stack<int, std::vector<int>> third; //empty stack with vector as underlying container
std::stack<int, std::vector<int>> fourth(myvector); //statck initialized to copy of vector with vector as underlying container std::cout << "size of first: " << first.size() << '\n';
std::cout << "size of second: " << second.size() << '\n';
std::cout << "size of third: " << third.size() << '\n';
std::cout << "size of fourth: " << fourth.size() << '\n'; system("pause");
return ;
}
/*
bool empty() const;
size_type size() const;
void push(const value_type& val);
void pop();
value_type& top();
*/
#include <iostream>
#include <stack> int main()
{
std::stack<int> mystack; for(int i=; i<; i++)
mystack.push(i*); std::cout<<"mystack containes: ";
while(!mystack.empty())
{
std::cout<<mystack.top()<<' ';
mystack.pop();
} std::cout<<'\n'; system("pause");
return ;
}
容器适配器之stack的更多相关文章
- C++STL模板库适配器之stack容器
目录 适配器 一丶适配器简介 二丶栈(stack)用法 1.栈的常用方法 适配器 一丶适配器简介 Stl中的适配器,有栈 (stack) 队列 queue 根priority_queue 适配器都是包 ...
- 容器适配器之priority_queue
template <class T, class Container = vector<T>, class Compare = less<type ...
- 容器适配器之queue
转载http://blog.csdn.net/thefutureisour/article/details/7751846容器适配器容器适配器其实就是一个接口转换装置,使得我们能用特定的方法去操作一些 ...
- C++STL模板库适配器之queue队列
目录 适配器之队列 一丶队列简介 二丶队列(queue)代码操作 1.常用方法 适配器之队列 一丶队列简介 队列是先进先出的数据结构. 在STL中使用 queue表示. 底层使用的是序列容器deque ...
- C++STL模板库适配器之优先级队列
目录 适配器之优先级队列 一丶优先级队列简介(priority_queue) 二丶优先级队列代码演示 1.优先级队列代码以及使用简介 适配器之优先级队列 一丶优先级队列简介(priority_queu ...
- 容器适配器(stack、 queue 、priority_queue)源码浅析与使用示例
一.容器适配器 stack queue priority_queue stack.queue.priority_queue 都不支持任一种迭代器,它们都是容器适配器类型,stack是用vector/d ...
- stl容器学习——queue,stack,list与string
目录 头文件 string 目录部分 1.string的定义及初始化 ① 用一个字符串给另一个字符串赋值 ②用字符串常量对字符串进行赋值 ③ 用n个相同的字符对字符串赋值 2.string的运算符及比 ...
- C++ STL容器之 stack
STL 中的 stack 是一种容器适配器,而不是一种容器. 它是容器适配器是指,只要支持一系列方法的容器(empty, size, back, push_back, pop_back),都能作为st ...
- C++容器简介1—stack
一.简介 stack是一种容器适配器(STL的容器分为顺序容器和关联容器,容器适配器),被设计来用于操作先进后出(FILO)结构的情景,在这种情况下, 元素的插入和删除都只能在容器的尾部进行. sta ...
随机推荐
- EnCase v.s. FTK - find out Chinese characters writing in different direction
A friend of mine said to me that she could fool those forensic tools easily by changing writing dire ...
- virtual box 中两个虚拟机 、宿主机 三机互通并且能上外网设置
virtual box 中两个虚拟机 .宿主机 三机互通并且能上外网设置 1:背景:因为需要学习linux,所以需要在虚拟机里装linux系统,测试要么宿主机与虚拟机linux网络实验测试:要么另一台 ...
- HTML5和CSS3的能量究竟是怎样的?
Web设计师可以使用HTML4和CSS2.1完成一些很酷的东西.我们可以在不使用陈旧的基于table布局的基础上完成文档逻辑结构并创建内容丰富的网站.我们可以在不使用内联<font>和&l ...
- Ubuntu 14.04 – How to install xrdp in Ubuntu 14.04
http://c-nergy.be/blog/?p=5305 Hello World, Ubuntu 14.04 has been released on April 17th 2014 and we ...
- PHP总结
1.PHP的简介: PHP(外文名:PHP: Hypertext Preprocessor,中文名:"超文本预处理器") 是一种通用开源脚本语言.语法吸收了C语言.Java和Per ...
- ASP.NET中@Page指令中的AutoEventWireup
AutoEventWireup:指示控件的事件是否自动匹配 (Autowire).如果启用事件自动匹配,则为 true:否则为 false.默认值为 true.如果设为false,则事件不可用.有关更 ...
- C#实现图书馆程序导入ISO-2709格式(MARC)功能
1.导入 /// <summary> /// 导入ISO2709 /// </summary> /// <param name="sender"> ...
- Web调用ExE
把这个文件导入注册表,那么我们在浏览器中输入PrintLabel://1022,那么就会自动调用C:\\Program Files\\xxx有限公司\\Sellercube Label Maker\\ ...
- js-布尔值
1.任何JavaScript的值都可以转换为布尔值 下面这些将会转换为false(假值): undefined null 0 -0 NaN "" //空字符串 所有其他值,包括所有 ...
- 如何在Netbeans中查看TODO项
以下要说的内容可能不起眼,但本人在找的时候着实费了一番功夫,个人感觉网上说的不着点,就在这儿有针对性地记录下来操作流程吧. 关于TODO的作用这里不做说明,在IDE中编写代码时,我们总会用到TODO, ...