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 Day26进程01天
Java开启多个线程有两种方法,一种继承Thread类,一种实现Runnable接口.具体示例如下: 01继承Thread类 02实现Runnable接口
- python入门(二):isinstance、内置函数、常用运算等
1. isinstance(变量名,类型) #判断什么类型 ps: 只支持输入两个参数,输入3个参数会报错 >>> isin ...
- 【Django】关于数据过滤
学到关于数据库过滤方面的内容总结部分注意点: views.py def TestFilter(request): # 多条件过滤 # list=BookInfo.book_manager.filter ...
- Tomcat常用设置及安全管理规范
前言 随着公司内部使用Tomcat作为web应用服务器的规模越来越大,为保证Tomcat的配置安全,防止信息泄露,恶性攻击以及配置的安全规范,特制定此Tomcat安全配置规范.注意: 本文章从htt ...
- ExecuteNonQuery()
ExecuteNonQuery():执行一个SQL语句,返回受影响的行数,这个方法主要用于执行对数据库执行增加.更新.删除操作,注意查询的时候不是调用这个方法.用于完成insert,delete,up ...
- [剑指Offer]5-替换空格
链接 https://www.nowcoder.com/practice/9023a0c988684a53960365b889ceaf5e?tpId=13&tqId=11210&tPa ...
- FortiGate抓包 Sniffer
1.图形界面抓包 系统管理--网络--数据包捕获 选择添加好的数据捕获,点击"运行"开关抓包:抓取包后,可以点击"下载"将抓取的数据包保存的本地磁盘,可以用wi ...
- 337. House Robber III二叉树上的抢劫题
[抄题]: The thief has found himself a new place for his thievery again. There is only one entrance to ...
- swift hidesBottomBarWhenPushed 设置界面
方法一(推荐):一级界面push的时候设置,子页面无需设置 let vc = JYMyCommissionController() vc.hidesBottomBarWhenPushed = true ...
- 自定义View(四) ViewGroup 动态添加变长Tag标签 支持自动换行
欲实现如下效果: 思路很简单就2步: 1.测量出ViewGroup的大小 2.找出子View的位置 若要实现动态添加标签view,就要实现ViewGroup的onMeasure().onLayout( ...