容器适配器之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 ...
随机推荐
- Java Swing打猎射击游戏源码
代码如下 <font size="3">package Game; import java.awt.Graphics; import java.awt.Image; i ...
- oracle 数组类型
create or replace function my_test(p_str varchar2) return number as --普通变量 v_var ); --固定长度数组 type v_ ...
- oracle中=>是什么意思
=> 是 Oracle 中调用 存储过程的时候, 指定 参数名进行调用.一般是, 某些参数有默认值的时候,你需要跳过某些参数来进行调用.下面是具体的例子. 参数的默认值SQL> CREAT ...
- HTML 5 Canvas
HTML 5 Canvas HTML5 的 canvas 元素使用 JavaScript 在网页上绘制图像. <canvas id="myCanvas" width=&q ...
- wpMVVM模式绑定集合的应用
一.新建一个项目,命名为wpMVVMone,添加一个有关食品信息的类Food.CS,代码如下: public class Food { public string Name { get; set; } ...
- 记 移动端页面中莫名其妙的渲染BUG
问题描述: 在一个很简单的测试页面中 简单的两块布局,上下两块均没有单独设置字体大小,都用body继承的大小,即40px.我们现在给第一个块.fl 加上浮动 float:left; 另外一个块处于正 ...
- LVS+keepalived实现负载均衡
背景: 随着你的网站业务量的增长你网站的服务器压力越来越大?需要负载均衡方案!商业的硬件如F5又太贵,你们又是创业型互联公司如何有效节约成本,节省不必要 的浪费?同时实现商业硬件一样的 ...
- DevExpress GridControl 使用方法技巧 总结 收录整理
一.如何解决单击记录整行选中的问题 View->OptionsBehavior->EditorShowMode 设置为:Click 二.如何新增一条记录 ().gridView.AddNe ...
- 向Windows 日志管理器写入系统程序日志信息
标准样例代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Da ...
- 刀哥多线程笔记之gcd-02-block
block 概念 block 是 C 语言的 是一种数据类型,可以当作参数传递 是一组预先准备好的代码,在需要的时候执行 动画 block 回顾 self.demoView.center = CGPo ...