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的更多相关文章

  1. C++ std::priority_queue

    std::priority_queue template <class T, class Container = vector<T>, class Compare = less< ...

  2. 容器适配器之priority_queue

    template <class T, class Container = vector<T>,                class Compare = less<type ...

  3. 【C++ 中文手册】即将完成

    [C++ 中文手册]即将完成 内容包含C++11,历时一年,日夜赶工,即将完成! 该参考手册主要由以下四部份内容组成: C++ 语言 C++ 继承了 C 语言 的大部分语法,并在其基础上修改或增加部分 ...

  4. most asked interview questions for C/C++

    1.   compared to prefix  ++, postfix increment needs one more step to create a temporary variable? w ...

  5. STL-容器库000

    容器库已经作为class templates 实现. 容器库中是编程中常用的结构: (1)动态数组结构vector: (2)队列queue: (3)栈stack: (4)heaps 堆priority ...

  6. c++的关联容器入门(map and set)

    目录 std::map std::set C++的关联容器主要是两大类map和set 我们知道谈到C++容器时,我们会说到 顺序容器(Sequence containers),关联容器(Associa ...

  7. 在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 ...

  8. .Container与.container_fluid区别

    .Container与.container_fluid是bootstrap中的两种不同类型的外层容器,两者的区别是:.container 类用于固定宽度并支持响应式布局的容器..container-f ...

  9. 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 ...

随机推荐

  1. Java Day26进程01天

    Java开启多个线程有两种方法,一种继承Thread类,一种实现Runnable接口.具体示例如下: 01继承Thread类 02实现Runnable接口

  2. python入门(二):isinstance、内置函数、常用运算等

    1.    isinstance(变量名,类型)                           #判断什么类型 ps: 只支持输入两个参数,输入3个参数会报错 >>> isin ...

  3. 【Django】关于数据过滤

    学到关于数据库过滤方面的内容总结部分注意点: views.py def TestFilter(request): # 多条件过滤 # list=BookInfo.book_manager.filter ...

  4. Tomcat常用设置及安全管理规范

    前言 随着公司内部使用Tomcat作为web应用服务器的规模越来越大,为保证Tomcat的配置安全,防止信息泄露,恶性攻击以及配置的安全规范,特制定此Tomcat安全配置规范.注意:  本文章从htt ...

  5. ExecuteNonQuery()

    ExecuteNonQuery():执行一个SQL语句,返回受影响的行数,这个方法主要用于执行对数据库执行增加.更新.删除操作,注意查询的时候不是调用这个方法.用于完成insert,delete,up ...

  6. [剑指Offer]5-替换空格

    链接 https://www.nowcoder.com/practice/9023a0c988684a53960365b889ceaf5e?tpId=13&tqId=11210&tPa ...

  7. FortiGate抓包 Sniffer

    1.图形界面抓包 系统管理--网络--数据包捕获 选择添加好的数据捕获,点击"运行"开关抓包:抓取包后,可以点击"下载"将抓取的数据包保存的本地磁盘,可以用wi ...

  8. 337. House Robber III二叉树上的抢劫题

    [抄题]: The thief has found himself a new place for his thievery again. There is only one entrance to ...

  9. swift hidesBottomBarWhenPushed 设置界面

    方法一(推荐):一级界面push的时候设置,子页面无需设置 let vc = JYMyCommissionController() vc.hidesBottomBarWhenPushed = true ...

  10. 自定义View(四) ViewGroup 动态添加变长Tag标签 支持自动换行

    欲实现如下效果: 思路很简单就2步: 1.测量出ViewGroup的大小 2.找出子View的位置 若要实现动态添加标签view,就要实现ViewGroup的onMeasure().onLayout( ...