特殊容器,又称为容器适配器(Container Adapter),它们改造了标准STL容器,使之满足特殊的要求。

Stack堆栈

使用stack时,需包含头文件<stack>

  • push()  将一个元素压入栈内
  • pop()   从栈内移除下一个元素,但是并不返回它
  • top()         返回栈内下一个元素,但并不移除它。

如果stack内没有元素,top()和pop()会导致不明确的行为,可采用size()或empty()来检查容器是否为空。

Queue队列

Queue实现出了一个FIFO先进先出的结构,是一个典型的数据缓冲构造。使用时需包含头文件<queue>

  • push() 将一个元素入队列
  • front()返回队列中第一个元素,但不移除元素
  • back()返回队列中最后一个元素,但不移除元素
  • pop()从队列中移除一个元素,但不返回它

如果队列内没有元素,front(),back()和pop()将导致不明确的行为,可采用size()或empty()来检查容器是否为空。

Priority Queue优先级队列

priority queue内的元素根据优先级进行了排序,使用时需包含头文件<queue>

  • push()将一个元素放入priority queue中
  • top()返回priority queue中第一个元素,但并不移除
  • pop()移除一个元素,但并不返回

如果优先级队列内没有元素,top()和pop()会导致不明确的行为,可采用size()或empty()来检查容器是否为空。

#include <iostream>
#include <queue>
using namespace std;int main()
{priority_queue<float> q;q.push(66.6);q.push(22.2);q.push(44.4);cout << q.top() << endl;q.pop();cout << q.top() << endl;q.pop();q.push(11.1);q.push(55.5);q.push(33.3);while (!q.empty()){cout << q.top() << endl;q.pop();}return 0;
}

View Code

BitSet

BitSet构造出了一个内含bit或Boolean值且大小固定的array,当需要管理各式flag,并以flag任意组合来表现变量时,就可以运用bitset。

BitSet定义于头文件<bitset>中

/* The following code example is taken from the book
* "The C++ Standard Library - A Tutorial and Reference, 2nd Edition"
* by Nicolai M. Josuttis, Addison-Wesley, 2012
*
* (C) Copyright Nicolai M. Josuttis 2012.
* Permission to copy, use, modify, sell and distribute this software
* is granted provided this copyright notice appears in all copies.
* This software is provided "as is" without express or implied
* warranty, and with no claim as to its suitability for any purpose.
*/
#include <bitset>
#include <iostream>
using namespace std;int main()
{// enumeration type for the bits// - each bit represents a colorenum Color {red, yellow, green, blue, white, black, //...,
        numColors};// create bitset for all bits/colorsbitset<numColors> usedColors;// set bits for two colorsusedColors.set(red);usedColors.set(blue);// print some bitset datacout << "bitfield of used colors:   " << usedColors << endl;cout << "number   of used colors:   " << usedColors.count() << endl;cout << "bitfield of unused colors: " << ~usedColors << endl;// if any color is usedif (usedColors.any()) {// loop over all colorsfor (int c = 0; c < numColors; ++c) {// if the actual color is usedif (usedColors[(Color)c]) {//...
            }}}
}

View Code

转载于:https://www.cnblogs.com/larry-xia/p/9504007.html

【C++标准库】特殊容器相关推荐

  1. C++知识点25——使用C++标准库(容器适配器stack、queue、priority_queue)

    除了vector,list,deque等常用的容器,还有根据这些常用的容器进行改造来满足特殊要求的容器,这些特殊容器的行为和常用容器很相近,也称为容器适配器. 常用的容器适配器有三个,分别是stack ...

  2. C++“准”标准库Boost学习指南(1):智能指针Boost.smart_ptr

    我们学习C++都知道智能指针,例如STL中的std::auto_ptr,但是为什么要使用智能指针,使用它能带给我们什么好处呢? 最简单的使用智能指针可以不会因为忘记delete指针而造成内存泄露.还有 ...

  3. C++_STL标准库——容器

    C++_STL标准库--容器 参考:cplusplus.com - The C++ Resources Network,VC2019,<C++primer>,<侯捷泛化编程与标准库& ...

  4. C++知识点33——使用C++标准库(无序关联容器unordered_(multi)map,unordered_(multi)set)

    C++中,无序关联容器一共有4个,unordered_map,unordered_set,unordered_multimap,unordered_multiset 这四个和有序关联容器最大的区别就是 ...

  5. C++知识点32——使用C++标准库(关联容器set和multiset的初始化,赋值,查找,添加,删除与迭代器失效)

    关联容器map和multimap已经在博客https://blog.csdn.net/Master_Cui/article/details/108690877和https://blog.csdn.ne ...

  6. C++知识点31——使用C++标准库(关联容器multimap及其初始化,赋值,查找,添加,删除与迭代器失效)

    关于关联容器map已经在博客https://blog.csdn.net/Master_Cui/article/details/108690877中介绍完了 multimap和map非常类似,容器中的元 ...

  7. C++知识点30——使用C++标准库(关联容器map及其初始化,赋值,查找,添加,删除与迭代器失效)

    一.关联容器简介 关于顺序容器和关联容器的区别已经在博客https://blog.csdn.net/Master_Cui/article/details/107427911中提过 C++标准库中的关联 ...

  8. C++知识点24——使用C++标准库(顺序容器deque的初始化,赋值,访问,添加,删除,交换与迭代器失效)

    deque容器是双端队列,使用前,需要添加#include <deque> deque的内存结构如下: 根据上图可知,deque和vector,string稍有不同,deque的内存是分段 ...

  9. C++知识点22——使用C++标准库(顺序容器list的初始化、赋值、访问、交换、添加、删除与迭代器失效)

    list容器是双向链表,使用前,需要添加#include <list> 1.list的初始化 常用的构造函数如下 explicit list (const allocator_type&a ...

  10. 标准模板库之容器-《C++标准库(第二版)》读书笔记

    写在前面:本文是阅读<C++标准库(第二版)>的读书笔记. 文章目录 6.1 STL组件(Component) 6.2 容器(Container) 6.2.1 序列式容器(Sequence ...

最新文章

  1. pandas index 用法 查询
  2. OO_Unit2_多线程电梯
  3. [云炬创业管理笔记]第6章制定创业行动测试5
  4. Spring quartz 并发性研究
  5. linux C++ 多线程使用pthread_cond 条件变量
  6. js中的事件循环和宏任务和微任务的理解
  7. java identity_仔细研究Java Identity API
  8. Java SecurityManager checkListen()方法与示例
  9. FPGA/CPLD状态机稳定性研究
  10. LR通过SiteScope监控mysql
  11. 【Java基础总结】网络编程
  12. 马哥linux第六周作业
  13. mysql锁表查询_Mysql upate 更新锁表还是锁行测试
  14. 据说IE7.0不支持跨域名脚本,那网页计数器不是要失效啦?
  15. Confluence 6 数据库表-杂项(Miscellaneous)
  16. 施耐德 m340 编程手册_施耐德电气自动化软件汇总
  17. jconsole远程连接的使用
  18. arm mali 天梯图_最全处理器排名:2017年处理器天梯图
  19. 前端工程中常用的文件夹命名(扫盲帖)
  20. 5G推动下,XR的需求“爆发”会来自B端还是C端?...

热门文章

  1. Mac 技术篇-苹果笔记本休眠启动后WIFI连接转圈卡死置灰不可用解决方法,mac通过终端杀进程实例演示
  2. Python 技术篇-如何打印一段文字,用友云霸气控制台颜文字打印
  3. Python 技术篇 - 查看python库都包含什么方法,查看python模块某个方法的具体用法源码,查看python模块所在的物理位置,查看python库都包含哪些属性
  4. 合并a[0..mid]和a[mid+1,n-1],其中这两个数组分别有序
  5. c++在txt中每行写入数据和每行输出
  6. 图像窗口方面 window
  7. pandas.Series.multiply()含义解释
  8. [简单题]换一个思维,代码简洁度就完全变了(Python实现)
  9. 使用OpenVINO遇到No name 'IENetwork' in module 'openvino.inference_engine'解决
  10. html 自定义js,js 自定义事件