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. 0初识Linux

    今天三八妇女节,Linux就该这么学,开课第一天.信心满满,激动,期待,要努力了.(博客为预习写的,今天又做了更新.)   Linux第一印象就是黑色背景屏幕,上面还有好多代码,敲的一手好的命令操控着 ...

  2. Spring用了哪些设计模式

    单例:只产生一个对象,共享对象的资源: 多例:产生多个对象,对象资源没有联系:(action) 在ssm框架中 service层.dao层.controller层都是默认使用单例模式,只会产生唯一 一 ...

  3. linux 硬链接 软链接

    硬链接 链接文件的INode和源文件相同,删除互不影响,不支持跨分区,不支持目录 软链接 链接文件有单独的INode,其Block中存储源文件的INode信息,相当于windows中 的快捷方式 ln ...

  4. 为docker私有registry配置nginx反向代理

    公司的Docker私有registry已经搭建好了,用官方的registry image很容易就搭建好了.现在就是要用nginx的反向代理把它放出来,以便在外网可以访问. 我的上一篇blog 讲了如何 ...

  5. SpringBoot打成jar包的配置方式

    pom.xml中添加Springboot插件 <build> <plugins> <plugin> <groupId>org.springframewo ...

  6. 使用::befor和::after伪元素在网站中添加图标

    css3为了区分伪类和伪元素,伪元素采用双冒号写法. 常见伪类——:hover,:link,:active,:target,:not(),:focus. 常见伪元素——::first-letter,: ...

  7. 20175314 《Java程序设计》第一周学习总结

    20175314 <Java程序设计>第一周学习总结   教材学习内容总结       除了学院统一购买的<Java 2 实用教程(第5版)>我还在网上买了一本<Head ...

  8. python的序列类

    1,我们常见的数据结构有哪些是序列类 序列类型的分类: ①  容器序列:list,tuple,deque(可以防止任意的类型的容器) ②  扁平序列:str,bytes,bytearray,array ...

  9. CentOS 下用 Nginx 和 uwsgi 部署 flask 项目

    前几天利用flask 写了几个调用salt-api 的接口,需要上线到正式环境,搜了一下 都是 用 nginx + uwsgi 来部署,这里记录下关键的配置项. 1.首先将代码上传到服务器上目录为: ...

  10. printf 字符串格式化

    在将各种类型的数据构造成字符串时,sprintf 的强大功能很少会让你失望.由于sprintf 跟printf 在用法上几乎一样,只是打印的目的地不同而已,前者打印到字符串中,后者则直接在命令行上输出 ...