Container Adaptors
Notes from C++ Primer
- stack and queue: based on deque
- priority_queue: based on vector
Standard library provides three kinds of sequential adaptors: queue, priority_queue and stack. When we use adaptor, we must include the relevant head files:
#include <stack>
#include <queue> // both queue and priority_queue adaptors
We can use two ways to initialize the adaptor: default constructor or a constructor with one container parameter:
deque<int> deq;
stack<int> stk(deq); // copies elements from deq into stk
Also we can override the underlying container type by passing a sequential container type:
// empty stack implemented on top of vector
stack<string, vector<string> > str_stk; // str_stk2 is implemented on top of vector and holds a copy of svec
stack<string, vector<string> > str_stk2(svec);
There're constraints on which containers can be used for a given adaptor.
- stack: any sequential container: vector, list, deque
- queue: the underlying container must support push_front, so only based on: list
- priority_queue: the underlying container should support random access, so based on: vecotr or deque
Stack Adaptor Operation
- s.empty() if the stack is empty, return true
- s.size() return the size of elements in the stack
- s.pop() delete the top element of stack, NOT return its value
- s.top() return the value of top element in stack, not deleting
- s.push(item) push new element in stack
Codes below show the five operations:
// number of elements we'll put in our stack
const stack<int>::size_type stk_size = 10;
stack<int> intStack; // empty stack // fill up the stack
int ix = 0;
while(intStack.size() != stk_size)
// use postfix increment; want to push old value onto intStack
intStack.push_back(ix++); // intStack holds 0...9 inclusive int error_cnt = 0;
while(intStack.empty() == false)
{
int value = intStack.top(); // read the top element of the stack
if(value != --ix)
{
cerr << "oops! expected " << ix
<< " received " << value << endl;
++error_cnt;
}
intStack.pop(); // pop the top element, and repeat
} cout << "Our program ran with "
<< error_cnt << " errors!" << endl;
Container Adaptors的更多相关文章
- C++ std::priority_queue
std::priority_queue template <class T, class Container = vector<T>, class Compare = less< ...
- 容器适配器之priority_queue
template <class T, class Container = vector<T>, class Compare = less<type ...
- 【C++ 中文手册】即将完成
[C++ 中文手册]即将完成 内容包含C++11,历时一年,日夜赶工,即将完成! 该参考手册主要由以下四部份内容组成: C++ 语言 C++ 继承了 C 语言 的大部分语法,并在其基础上修改或增加部分 ...
- most asked interview questions for C/C++
1. compared to prefix ++, postfix increment needs one more step to create a temporary variable? w ...
- STL-容器库000
容器库已经作为class templates 实现. 容器库中是编程中常用的结构: (1)动态数组结构vector: (2)队列queue: (3)栈stack: (4)heaps 堆priority ...
- c++的关联容器入门(map and set)
目录 std::map std::set C++的关联容器主要是两大类map和set 我们知道谈到C++容器时,我们会说到 顺序容器(Sequence containers),关联容器(Associa ...
- 在docker中运行ASP.NET Core Web API应用程序(附AWS Windows Server 2016 widt Container实战案例)
环境准备 1.亚马逊EC2 Windows Server 2016 with Container 2.Visual Studio 2015 Enterprise(Profresianal要装Updat ...
- .Container与.container_fluid区别
.Container与.container_fluid是bootstrap中的两种不同类型的外层容器,两者的区别是:.container 类用于固定宽度并支持响应式布局的容器..container-f ...
- View and Data API Tips: Constrain Viewer Within a div Container
By Daniel Du When working with View and Data API, you probably want to contain viewer into a <div ...
随机推荐
- js跳转新窗口
语法:window.open(url,[target]); eg: window.open("index.html"); window.open("index.html& ...
- LNMP 支持 ThinkPHP 的 pathinfo 模式
注意使用LNMP 1.4版 1.修改php.ini 启用pathinfo /usr/local/php/etc/php.ini cgi.fix_pathinfo = 0 值改为1 2.修改/usr/l ...
- 深度学习原理与框架-递归神经网络-RNN网络基本框架(代码?) 1.rnn.LSTMCell(生成单层LSTM) 2.rnn.DropoutWrapper(对rnn进行dropout操作) 3.tf.contrib.rnn.MultiRNNCell(堆叠多层LSTM) 4.mlstm_cell.zero_state(state初始化) 5.mlstm_cell(进行LSTM求解)
问题:LSTM的输出值output和state是否是一样的 1. rnn.LSTMCell(num_hidden, reuse=tf.get_variable_scope().reuse) # 构建 ...
- 高性能迷你React框架anujs1.0.5发布
实现对createFactory的支持,优化scheduler与dispose机制,提供ReactShim文件,跑通公司内部4套测试 npm i anujs 或者使用架手架 https://githu ...
- (译)内存沉思:多个名称相关的神秘的SQL Server内存消耗者。
原文出处:https://blogs.msdn.microsoft.com/sqlmeditation/2013/01/01/memory-meditation-the-mysterious-sql- ...
- 2017-09-16 ADB Shell+Putty
鼓捣电子词典的时候需要用到ADB Shell.一开始是用cmd.exe,结果发现它不能识别ANSI转义符,就换成了Putty,然后就可以正常使用了,还有彩色. 配置如下: Connection Typ ...
- VUE - 相对路径
background-image背景图片路径问题可以这样解决:1.先在data里面导入这张图片,例如: bg:require('./openIndexBG2.jpg')2 ...
- nginx实现http www服务的方式
- [剑指Offer]38-字符串的全排列
链接 https://www.nowcoder.com/practice/fe6b651b66ae47d7acce78ffdd9a96c7?tpId=13&tqId=11180&tPa ...
- python --数据可视化(一)
python --数据可视化 一.python -- pyecharts库的使用 pyecharts--> 生成Echarts图标的类库 1.安装: pip install pyecharts ...