c++11 标准模板(STL)(std::queue)(五)

news2024/10/6 12:30:04
定义于头文件 <queue>
template<

    class T,
    class Container = std::deque<T>

> class queue;

std::queue 类是容器适配器,它给予程序员队列的功能——尤其是 FIFO (先进先出)数据结构。

类模板表现为底层容器的包装器——只提供特定的函数集合。 queue 在底层容器尾端推入元素,从首端弹出元素。

成员对象

底层容器

Container c


非成员函数

按照字典顺序比较 queue 中的值

operator==,!=,<,<=,>,>=(std::queue)
template< class T, class Container >

bool operator==( const queue<T,Container>& lhs,

                 const queue<T,Container>& rhs );
(1)
template< class T, class Container >

bool operator!=( const queue<T,Container>& lhs,

                 const queue<T,Container>& rhs );
(2)
template< class T, class Container >

bool operator<( const queue<T,Container>& lhs,

                const queue<T,Container>& rhs );
(3)
template< class T, class Container >

bool operator<=( const queue<T,Container>& lhs,

                 const queue<T,Container>& rhs );
(4)
template< class T, class Container >

bool operator>( const queue<T,Container>& lhs,

                const queue<T,Container>& rhs );
(5)
template< class T, class Container >

bool operator>=( const queue<T,Container>& lhs,

                 const queue<T,Container>& rhs );
(6)

 比较二个容器适配器的底层容器。通过应用对应的运算符到底层容器进行比较。

参数

lhs, rhs-要比较内容的迭代器适配器
- T 必须满足可相等比较 (EqualityComparable) 的要求。

返回值

若对应比较产出 true 则为 true ,否则为 false 。

复杂度

与容器大小成线性

特化 std::swap 算法

std::swap(std::queue)
template< class T, class Container >

void swap( queue<T,Container>& lhs,

           queue<T,Container>& rhs );
(C++17 前)
template< class T, class Container >

void swap( queue<T,Container>& lhs,

           queue<T,Container>& rhs ) noexcept(/* see below */);
(C++17 起)

 为 std::queue 特化 std::swap 算法。交换 lhsrhs 的内容。调用 lhs.swap(rhs) 。

此重载仅若 std::is_swappable<Container>::value 为 true 才参与重载决议。

(C++17 起)

参数

lhs, rhs-要交换内容的容器

返回值

(无)

复杂度

与交换底层容器相同。

异常

noexcept 规定:  

noexcept(noexcept(lhs.swap(rhs)))

(C++17 起)

 

辅助类

特化 std::uses_allocator 类型特性

std::uses_allocator<std::queue>
template< class T, class Container, class Alloc >

struct uses_allocator<queue<T,Container>,Alloc> :

    std::uses_allocator<Container, Alloc>::type { };
(C++11 起)

为 std::queue 提供 std::uses_allocator 类型特性的通透特化:容器适配器使用分配器,若且唯若底层容器使用。

继承自 std::integral_constant

成员常量

value

[静态]

true
(公开静态成员常量)

成员函数

operator bool

转换对象为 bool ,返回 value
(公开成员函数)

operator()

(C++14)

返回 value
(公开成员函数)

成员类型

类型定义
value_typebool
typestd::integral_constant<bool, value>

 

调用示例

#include <iostream>
#include <forward_list>
#include <string>
#include <iterator>
#include <algorithm>
#include <functional>
#include <queue>
#include <deque>
#include <time.h>

using namespace std;

struct Cell
{
    int x;
    int y;

    Cell() = default;
    Cell(int a, int b): x(a), y(b) {}

    Cell &operator +=(const Cell &cell)
    {
        x += cell.x;
        y += cell.y;
        return *this;
    }

    Cell &operator +(const Cell &cell)
    {
        x += cell.x;
        y += cell.y;
        return *this;
    }

    Cell &operator *(const Cell &cell)
    {
        x *= cell.x;
        y *= cell.y;
        return *this;
    }

    Cell &operator ++()
    {
        x += 1;
        y += 1;
        return *this;
    }


    bool operator <(const Cell &cell) const
    {
        if (x == cell.x)
        {
            return y < cell.y;
        }
        else
        {
            return x < cell.x;
        }
    }

    bool operator >(const Cell &cell) const
    {
        if (x == cell.x)
        {
            return y > cell.y;
        }
        else
        {
            return x > cell.x;
        }
    }

    bool operator ==(const Cell &cell) const
    {
        return x == cell.x && y == cell.y;
    }
};

std::ostream &operator<<(std::ostream &os, const Cell &cell)
{
    os << "{" << cell.x << "," << cell.y << "}";
    return os;
}

void queuePrint(const std::string &name, const std::queue<Cell> &queue)
{
    std::cout << name ;
    std::queue<Cell> queuep = queue;
    while (queuep.size() > 0)
    {
        std::cout << queuep.front() << " ";
        queuep.pop();
    }
    std::cout << std::endl;
}

int main()
{
    std::cout << std::boolalpha;

    std::mt19937 g{std::random_device{}()};
    srand((unsigned)time(NULL));

    auto generate = []()
    {
        int n = std::rand() % 10 + 110;
        Cell cell{n, n};
        return cell;
    };

    std::deque<Cell> deque1(6);
    std::generate(deque1.begin(), deque1.end(), generate);
    std::cout << "deque1:   ";
    std::copy(deque1.begin(), deque1.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::deque<Cell> deque2(6);
    std::generate(deque2.begin(), deque2.end(), generate);
    std::cout << "deque2:   ";
    std::copy(deque2.begin(), deque2.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;

    //2) 以 cont 的内容复制构造底层容器 c 。
    std::queue<Cell> queue1(deque1);
    std::queue<Cell> queue2(deque1);
    queuePrint("queue1:   ", queue1);
    queuePrint("queue2:   ", queue2);

    std::queue<Cell> queue3(deque2);
    queuePrint("queue3:   ", queue3);
    std::cout << std::endl;

    //比较二个容器适配器的底层容器。通过应用对应的运算符到底层容器进行比较。
    std::cout << "queue1 == queue2: " << (queue1 == queue2) << std::endl;
    std::cout << "queue1 == queue3: " << (queue1 == queue3) << std::endl;
    std::cout << "queue1 != queue2: " << (queue1 != queue2) << std::endl;
    std::cout << "queue1 != queue3: " << (queue1 != queue3) << std::endl;
    std::cout << "queue1 <  queue2: " << (queue1 <  queue2) << std::endl;
    std::cout << "queue1 <  queue3: " << (queue1 <  queue3) << std::endl;
    std::cout << "queue1 <= queue2: " << (queue1 <= queue2) << std::endl;
    std::cout << "queue1 <= queue3: " << (queue1 <= queue3) << std::endl;
    std::cout << "queue1 >  queue2: " << (queue1 >  queue2) << std::endl;
    std::cout << "queue1 >  queue3: " << (queue1 >  queue3) << std::endl;
    std::cout << "queue1 >= queue2: " << (queue1 >= queue2) << std::endl;
    std::cout << "queue1 >= queue3: " << (queue1 >= queue3) << std::endl;
    std::cout << std::endl;

    //为 std::queue 特化 std::swap 算法。交换 lhs 与 rhs 的内容。
    std::cout << "swap before:  " << std::endl;
    queuePrint("queue1:   ", queue1);
    queuePrint("queue3:   ", queue3);
    std::swap(queue1, queue3);
    std::cout << "swap after:  " << std::endl;
    queuePrint("queue1:   ", queue1);
    queuePrint("queue3:   ", queue3);
}

输出

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/443615.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

kali相关操作

目录 kali换源&#xff1a; 由于没有公钥&#xff0c;无法验证下列签名&#xff1a; NO_PUBKEY 871920D1991BC93C 安装中文 设置中文输入法 kali换源&#xff1a; sudo su 备份相关配置 cp /etc/apt/sources.list /etc/apt/sources.list.bak vim /etc/apt/sources.list替…

搭建SFTP服务安全共享文件,实现在外远程访问「内网穿透」

文章目录 1.前言2.本地SFTP服务器搭建2.1.SFTP软件的下载和安装2.2.配置SFTP站点2.3.Cpolar下载和安装 3.SFTP服务器的发布3.1.Cpolar云端设置3.2.Cpolar本地设置 4.公网访问测试5.结语 1.前言 现在的网络发达&#xff0c;个人电脑容量快速上升&#xff0c;想要保存的数据资料…

最近,我们做了一次“实景”容灾演练

保障头部新闻客户端的业务连续性&#xff0c;阿里云帮助客户在真实场景下完成容灾演练。 云计算和新闻 APP&#xff0c;能有什么关系&#xff1f; 2021 年&#xff0c;传媒行业某头部媒体的新闻客户端进行了全新改版并升级上线&#xff0c;以 “内容技术”的融合驱动效率提升&a…

Vue2-黑马(十)

目录&#xff1a; &#xff08;1&#xff09;vuex-actions &#xff08;2&#xff09;vuex-调用actions &#xff08;3&#xff09;vue2实战-项目搭建 &#xff08;1&#xff09;vuex-actions 有这么一个需求&#xff0c;绿色的组件从服务器获取数据放入store&#xff0c;主…

CHAPTER 2: 《BACK-OF-THE-ENVELOPE ESTIMATION》 第2章 《初略的估计》

CHAPTER 2: BACK-OF-THE-ENVELOPE ESTIMATION 在系统设计面试中&#xff0c;有时您会被要求估计系统容量或使用粗略估计的性能需求。根据杰夫迪恩的说法&#xff0c;谷歌高级研究员&#xff0c;“粗略的计算是你使用结合思想实验和常见的性能数字&#xff0c;以获得良好的感觉…

武器目标分配问题研究进展: 模型、算法与应用

源自&#xff1a;系统公正与电子技术 作者&#xff1a;李梦杰 常雪凝 石建迈 陈超 黄金才 刘忠 摘 要 武器目标分配问题是指挥控制与任务规划领域的关键难点之一, 也是军事运筹领域的基础研究课题。经过多年研究, 武器目标分配问题在陆海空天电等领域都得到了广泛研究,…

ESLint插件开发

ESLint 插件 ESLint 插件是一个可以包含一系列ESLint 规则、配置、处理器、环境的npm模块。 创建插件 开发ESLint插件可以使用Yeoman提供的生成器来生成ESLint插件的基本项目的目录结构。 安装yeoman 和ESLint 规则的生成器 npm i -g yo generator-eslint创建eslint-xxxx使用y…

春秋云境:CVE-2022-26201(二次注入漏洞)

目录 一、题目 二、上传权限马 三、蚁剑马连接 一、题目 进入题目&#xff1a; 这个是和春秋云境&#xff1a;CVE-2022-28060一个网站&#xff0c;看来应该是存在多个漏洞。 点击admin 不用登录 Users ---- add users 选择文件 这里要是jpg图片格式 如果是php格式是无法运行…

机器学习(一)-K近邻算法(KNN)原理剖析及python源码

本篇介绍第一个机器学习算法&#xff1a;k-近邻算法&#xff0c;它非常有效而且易于掌握。首先&#xff0c;我们将探讨k-近邻算法&#xff08;KNN&#xff09;的基本理论&#xff0c;以及如何使用距离测量的方法分类物品&#xff1b;其次我们将使用Python从文本文件中导入并解析…

JavaSE学习进阶day06_02 Set集合和Set接口

第二章 Set系列集合和Set接口 Set集合概述&#xff1a;前面学习了Collection集合下的List集合&#xff0c;现在继续学习它的另一个分支&#xff0c;Set集合。 set系列集合的特点&#xff1a; Set接口&#xff1a; java.util.Set接口和java.util.List接口一样&#xff0c;同样…

D. Marcin and Training Camp(思维 + 判断一个数二进制位是否是另一个数的子集)

Problem - D - Codeforces 马辛是他大学里的一名教练。有N个学生想参加训练营。马辛是个聪明的教练&#xff0c;所以他只想派那些能冷静合作的学生参加。 让我们关注一下这些学生。每个学生可以用两个整数ai和bi来描述&#xff1b;bi等于第i个学生的技能水平&#xff08;越高越…

【刷题之路】LeetCode 程序员面试金典 08.03. 魔术索引

【刷题之路】LeetCode 程序员面试金典 08.03. 魔术索引 一、题目描述二、解题1、方法1——暴力法1.1、思路分析1.2、代码实现 2、方法2——二分分治2.1、思路分析2.2、代码实现 一、题目描述 原题连接&#xff1a; 面试题 08.03. Magic Index LCCI 题目描述&#xff1a; 魔术索…

计算机网络考试复习——第三章 3.3

3.3 使用广播信道的数据链路层 3.3.1局域网的数据链路层 局域网最主要的特点&#xff1a; 1.网络为一个单位所拥有&#xff1b; 2.地理范围和站点数目均有限。 局域网具有如下主要优点&#xff1a; 1.具有广播功能&#xff0c;从一个站点可很方便地访问全网。 2.便于系统…

分布式问题,你知道几个?

你好&#xff0c;我是田哥 现在的单体服务是很难应付面试了&#xff0c;必须要把分布式相关技术给讲清楚&#xff0c;否则面试难搞。 下面我们来聊聊&#xff0c;分布式环境下会面临哪些问题。 先来看一下主要内容&#xff1a; 分布式系统中常见的难题包括&#xff1a; 一致性问…

PackageMS 启动

1.PackageMS 相关框架类 2.PackageMS 启动过程 2.1 # SystemServer.java /** 291 * The main entry point from zygote. 292 */ 293 public static void main(String[] args) { 294 new SystemServer().run(); 295 } 上面是SystemServer的主函数。 pr…

RK3568开发板 buildroot编译配置

启扬智能RK3568开发板提供两种文件系统&#xff0c;一种是Debian&#xff0c;另外一种是BuildRoot。在使用过程中&#xff0c;有些客户需要添加属于自己的软件包&#xff0c;所以在此分享启扬RK3568关于BuildRoot编译配置的方法以及相关注意事项。 启扬提供的源码是完整sdk&am…

毕业论文用什么流程图软件比较好?

在写作论文的时候使用流程图&#xff0c;会让我们的论文看起来更加有逻辑。并且流程图的图片都可以在PPT中随意插入以及使用。 基础流程图作为最为基本和简单的的流程图方式&#xff0c;一般不区分用户角色和场景&#xff0c;适用于简单场景&#xff0c;梳理单一的流程情况&am…

nssctf web入门(8)

目录 [SWPUCTF 2021 新生赛]easy_sql [SWPUCTF 2021 新生赛]error 这里通过nssctf的题单web安全入门来写&#xff0c;会按照题单详细解释每题。题单在NSSCTF中。 想入门ctfweb的可以看这个系列&#xff0c;之后会一直出这个题单的解析&#xff0c;题目一共有28题&#xff0c;…

mysql(8.0)_主从复制

1.环境介绍 自己的主机--master 同学的主机--slave2.安装mysql https://blog.csdn.net/weixin_45955039/article/details/130144515?spm1001.2014.3001.55013. 准备工作 3.1在云服务器上添加端口号 3.2关闭防火墙 systemctl stop firewalld setenforce 04.master上的配置 …