STL学习笔记--特殊容器
容器配接器
(1) stack
栈 后进先出(LIFO), 头文件#include<stack>
template<class _Ty,
class _Container = deque<_Ty> >
class stack
{ // LIFO queue implemented with a container
...
可以看出,stack内部缺省使用deque实现。可以使用任何序列式容器来支持stack,只要支持back(),push_back操作。可以使用list或vector实现。
stack核心接口:
push 压栈
top 返回栈顶元素,并不移除
pop 出栈 ,移除下一个元素,并不将它返回.
注:如果stack内没有元素,执行top pop会导致为定义的行为,要使用empty()或size()来检验是否为空。
stack运用实例:
//stack 栈 后进先出
stack<int> st;
st.push(1); //压栈
st.push(3);
st.push(8);
st.push(-10);
cout << "\nStack:";
while (!st.empty())
{
cout << st.top() << " "; //取栈顶元素
st.pop(); //出栈
}
(2) queue
队列 先进先出(FIFO), 头文件#include<queue>
queue内部也采用deque实现 支持front() back() push_back pop_front()操作的任何序列式容器都可以实现queue,如list。
queue接口:
push() 入队
front() 返回队首元素
pop 出队,移除元素
queue运用实例:
//queue 队列 先进先出
queue<int,list<int> > listQueue; //使用list实现queue
listQueue.push(1); //入队
listQueue.push(3);
listQueue.push(8);
listQueue.push(-10);
cout << "\nQueue:";
while (!listQueue.empty())
{
cout << listQueue.front() << " ";//取队首元素
listQueue.pop();//出队
}
(3) priority_queue 优先队列
优先级队列的元素根据优先级读取,即优先级队列的元素已按排序准则排序,缺省的排序准则operator<.
template<class _Ty,
class _Container = vector<_Ty>,
class _Pr = less<typename _Container::value_type> >
class priority_queue
{ // priority queue
...
接口:
push 入队
top 取下一个元素
pop 出队,移除元素
Bitsets
bitset造出一个内含位(bits)或boolean值且大小固定的array。当需要管理各式标志(flags),并以标识的组合来表现变量时,就可以运用bitset。
头文件#include<bitset>
template<size_t _Bits>
class bitset
...
注:这里 template参数并不是一个型别,而是一个不带负号的整数。
运用实例:
enum Color{
red,
yellow,
green,
blue,
white,
black,
numColors
};
bitset<numColors> usedColors;
usedColors.set(red);//置位
usedColors.set(yellow);
cout << "\nbitfield: " << usedColors;
cout << "\nnumber of used colors: " << usedColors.count(); //返回"位值为1"的个数
cout << "\nbitfield of unused colors: " << ~usedColors;
if (usedColors.any()) //判断是否有任何位被值1
{
cout <<"\nuse colors!";
}
if (usedColors.test(red)) //判断该位是否被设立
{
cout << "\nuse red color!";
}
usedColors.set(); //所有位设为true
usedColors.reset(); //所有位设为false
STL学习笔记--特殊容器的更多相关文章
- STL学习笔记(一) 容器
0.前言随机访问迭代器: vector.string.dequeSTL的一个革命性的方面就是它的计算复杂性保证 条款01:慎重选择容器类型 c++提供的容器:标准STL序列容器:vector.stri ...
- STL学习笔记— —无序容器(Unordered Container)
简单介绍 在头文件<unordered_set>和<unordered_map> 中定义 namespace std { template <typename T, ty ...
- STL学习笔记--各种容器的运用时机
如何选择最佳的容器类别? 缺省情况下应该使用vector.vector的内部结构简单,并允许随机存取,所以数据的存取十分方便灵活,数据的处理也够快. 如果经常要在序列的头部和尾部安插和移除元素,应采用 ...
- Effective STL 学习笔记 32 ~ 33
Effective STL 学习笔记 32 ~ 33 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...
- Effective STL 学习笔记 31:排序算法
Effective STL 学习笔记 31:排序算法 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...
- Effective STL 学习笔记 Item 30: 保证目标区间足够大
Effective STL 学习笔记 Item 30: 保证目标区间足够大 */--> div.org-src-container { font-size: 85%; font-family: ...
- Effective STL 学习笔记: Item 22 ~ 24
Effective STL 学习笔记: Item 22 ~ 24 */--> div.org-src-container { font-size: 85%; font-family: monos ...
- Effective STL 学习笔记 Item 21:Comparison Function 相关
Effective STL 学习笔记 Item 21:Comparison Function 相关 */--> div.org-src-container { font-size: 85%; f ...
- Effective STL 学习笔记:19 ~ 20
Effective STL 学习笔记:19 ~ 20 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...
随机推荐
- css笔记 - 张鑫旭css课程笔记之 margin 篇
margin - 人若犯我,我必犯人! [margin地址](https://www.imooc.com/learn/680) 一.margin与容器尺寸的关系 relative可定位,但是不改变容器 ...
- C#设计模式--工厂方法模式
0.C#设计模式-简单工厂模式 设计模式: 工厂方法模式(Factory Method Pattern) 介绍:简单工厂模式是要在工厂类中通过数据来做个决策,在工厂类中的多个类中实例化出来其中一个要用 ...
- wireshark和RawCap跟踪并解决中文乱码问题
一.问题概述 说下程序的架构. 有个后台管理系统A,在页面修改数据后,会用httpClient发http请求给系统B: 系统B做了异步机制,收到A发的请求后,将数据封装为Mq消息发给RabbitMq, ...
- Python拷贝文件脚本
author : headsen chen date : 2018-12-06 17:56:58 copy_file.py #!/usr/bin/env python from sys import ...
- host.conf 文件
/etc/host.conf文件的作用是设置名称解析时的先后顺序/etc/hosts文件是在使用host解析时,手动的添加的主机记录/etc/relov.conf文件中设置DNS服务器名称以及缺省的域 ...
- oracle如何设置表空间autoextensible自动扩容
SELECT a.tablespace_name "表空间名", total / 1024 / 1024 "表空间大小单位M", free / 1024 / 1 ...
- springMVC前后台交互
后台返回json对象: package com.sawshaw.controller; import org.springframework.stereotype.Controller; import ...
- 在 arc里面打印 引用计数的方法
查阅资料: You can use CFGetRetainCount with Objective-C objects, even under ARC: NSLog(@"Retain c ...
- CentOS7.5搭建Solr7.4.0集群服务
一.Solr集群概念 solr单机版搭建参考: https://www.cnblogs.com/frankdeng/p/9615253.html 1.概念 SolrCloud(solr 云)是Solr ...
- 使用sift特征点进行knn最近邻匹配
#include <opencv2/xfeatures2d/nonfree.hpp> #include <opencv2/features2d/features2d.hpp> ...