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 ...
随机推荐
- JAVA 没有重载运算符,那么 String 类型的加法是怎么实现的,以及String类型不可变的原因和好处
1, JAVA 不具备 C++ 和 C# 一样的重载运算符 来实现类与类之间相互计算 的功能 这其实一定程度上让编程失去了代码的灵活性, 但是个人认为,这在一定程度上减少了代码异常的概率 ...
- 最近面试 Java 后端的感受!
来源:cnblogs.com/JavaArchitect/p/10011253.html 上周,密集面试了若干位Java后端候选人,工作经验在3到5年间.我的标准其实不复杂: 第一能干活,第二Java ...
- 一个linux bbr存在的调用顺序bug
最近跟踪bbr的状态转换的代码,发现一个问题: [,min_rtt_us=,full_bw=,cycle_idx=,pacing_gain=,cwnd_gain=,rtt_cnt= [,min_rtt ...
- 《Spring_Four》第一次作业:团队亮相
part one: 1.队名:Spring_Four 2.团队成员组成:学号/姓名(标记团队组长) 201571030114 李蕾 201571030143 周甜甜 201571030139 张天旭( ...
- 微信开发 invalid openid
微信开发时候测试号运行正常,换到正式号就会报invalid openid的错误. 看了微信问答系统里的答案,说是json格式的问题,但是我这边不是这个原因. 后来突然想到了,应该是AppId和AppS ...
- IP路由配置之---------debugging调试
实验设备:华三设备N台加一个PC 步骤一,打开屏幕输出开关,开启控制台对系统信息的监视功能 <H3C>terminal debugging #<H3C>terminal mon ...
- yum源解释。。。。。
主要说明下如何配置linux上的本地yum源,主要关于一些原理上的说明. 1.yum是什么,yum源又是什么 在windows上安装一个软件,我们可以通过360管家.因为360管家提供了软 ...
- 716. Max Stack实现一个最大stack
[抄题]: Design a max stack that supports push, pop, top, peekMax and popMax. push(x) -- Push element x ...
- php curl请求回来的中文为乱码
在浏览器访问回来的编码格式是正常的,但是从php curl 请求过来的确实乱码,之前也试过这个函数好像不行,今天吧最后一个参数换了,简单粗暴,可以了mb_convert_encoding($res, ...
- 20172325 2018-2019-2 《Java程序设计》第六周学习总结
20172325 2018-2019-2 <Java程序设计>第六周学习总结 教材学习内容总结 本周学习第十章--树 1.什么是树 (1)树是一种数据结构,与之前学过的栈.队列和列表这些线 ...