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. maven pom文件

    setting.xml主要用于配置maven的运行环境等一系列通用的属性,是全局级别的配置文件:而pom.xml主要描述了项目的maven坐标,依赖关系,开发者需要遵循的规则,缺陷管理系统,组织和li ...

  2. leetcode46

    public class Solution { public IList<IList<int>> Permute(int[] nums) { IList<IList< ...

  3. React更新元素 基础

    React元素创建后无法修改其内容和属性.唯一的办法是创建新的元素,传入ReactDOM.render()方法 三种实现形式: 1.整体替换 function tick () { const ele= ...

  4. XAMPP与本地Mysql冲突解决方法

    1.更改regeit目录 https://blog.csdn.net/sinat_37633633/article/details/77645463 2.更改配置文件my.ini (1)https:/ ...

  5. JavaScript学习-3——数组、函数、递归

    本章目录 -----------①数组 -----------②函数 -----------③递归 一.数组 弱类型:任何类型数据,且没有强度限制: 强类型:同一类型的数据存储的集合(内存中连续存储) ...

  6. 《深入理解JAVA虚拟机》----------第二章 JAVA内存区域与内存溢出异常,笔记(下)

    2. HotSpot虚拟机对象探秘 2.1 对象的创建 虚拟机遇到一条New指令时,首先将去检查这个指令的参数是否能在常量池中定位到一个类的符号引用,并且检查这个符号引用代表的类是否已被加载.解析和初 ...

  7. 浅析AnyCast网络技术

    什么是BGP AnyCast? BGP anycast就是利用一个(多个) as号码在不同的地区广播相同的一个ip段.利用bgp的寻路原则,短的as path 会选成最优路径(bgp寻路原则之n),从 ...

  8. Python中import, from...import,import...as的区别

    import datetime print(datetime.datetime.now()) 以上代码实现输出系统当前时间,是引入整个datetime包,然后再调用datetime这个类中的now() ...

  9. Apache ab 压力并发测试工具

    当你使用PHP(或其他编程语言)完成一个web程序的开发,并且web程序在Apache服务器上正常运行的时候,你有没有考虑过对你的Apache服务器及部署在其上的web程序进行一些压力测试呢?毕竟,真 ...

  10. 890. Find and Replace Pattern找出匹配形式的单词

    [抄题]: You have a list of words and a pattern, and you want to know which words in words matches the ...