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

news/2024/7/5 3:55:17
定义于头文件 <queue>
template<

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

> class queue;

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

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

修改器

向队列尾部插入元素

std::queue<T,Container>::push

void push( const value_type& value );

void push( value_type&& value );

(C++11 起)

 推给定的元素 value 到 queue 尾。

1) 等效地调用 c.push_back(value)

2) 等效地调用 c.push_back(std::move(value))

参数

value-要推入的元素值

返回值

(无)

复杂度

等于 Container::push_back 的复杂度。

于尾部原位构造元素

std::queue<T,Container>::emplace

template< class... Args >
void emplace( Args&&... args );

(C++11 起)
(C++17 前)

template< class... Args >
decltype(auto) emplace( Args&&... args );

(C++17 起)

 推入新元素到 queue 结尾。原位构造元素,即不进行移动或复制操作。以与提供给函数者准确相同的参数调用元素的构造函数。

等效地调用 c.emplace_back(std::forward<Args>(args)...); 。

参数

args-转发给元素构造函数的参数

返回值

(无)(C++17 前)
上述对 Container::emplace_back 的调用返回的值或引用,若它存在。(C++17 起)

复杂度

等同于 Container::emplace_back 的复杂度。

删除栈顶元素

std::queue<T,Container>::pop

void pop();

从 queue 移除前端元素。等效地调用 c.pop_front() 。

参数

(无)

返回值

(无)

复杂度

等于 Container::pop_front 的复杂度。

交换内容

std::queue<T,Container>::swap

void swap( queue& other ) noexcept(/* see below */);

(C++11 起)

交换容器适配器与 other 的内容。等效地调用 using std::swap; swap(c, other.c); 。

参数

other-要交换内容的容器适配器

返回值

(无)

示例

noexcept 规定:  

noexcept(noexcept(swap(c, other.c)))

上述表达式中,用与 C++17 std::is_nothrow_swappable 特性所用的相同方式查找标识符 swap

(C++17 前)
noexcept 规定:  

noexcept(std::is_nothrow_swappable<Container>::value)

(C++17 起)

复杂度

与底层容器相同(典型地为常数)。

调用示例

#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;

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

    while (!queue1.empty())
    {
        //返回到 queue 中首元素的引用。
        //此元素将是调用 pop() 时第一个移除的元素。等效地调用 c.front() 。
        //const_reference
        std::cout << "queue1.front() before: " << queue1.front() << " ";
        //reference
        queue1.front() = generate();
        std::cout << "after: " << queue1.front();
        //从 queue 移除顶元素。等效地调用 c.pop_back()
        queue1.pop();
        std::cout << std::endl;
    }
    std::cout << std::endl;

    std::queue<Cell> queue2;
    int index = 0;
    while (index < 6)
    {
        Cell cell = generate();
        if (queue2.size() % 2 == 0)
        {
            //推给定的元素 value 到 queue 尾。
            //1) 等效地调用 c.push_back(value)
            queue2.push(cell);
        }
        else
        {
            //推给定的元素 value 到 queue 尾。
            //2) 等效地调用 c.push_back(std::move(value)),移动语义
            queue2.push(std::move(cell));
        }
        std::cout << "queue2.front() : " << queue2.front() ;
        queue2.pop();
        std::cout << std::endl;
        index++;
    }
    std::cout << std::endl;

    std::queue<Cell> queue3;
    index = 0;
    while (index < 6)
    {
        int n = std::rand() % 10 + 110;
        //推入新元素到 queue 结尾。原位构造元素,即不进行移动或复制操作。
        //以与提供给函数者准确相同的参数调用元素的构造函数。
        //等效地调用 c.emplace_back(std::forward<Args>(args)...);
        queue3.emplace(n, n);
        std::cout << "queue3.front() : " << queue3.front() ;
        std::cout << std::endl;
        queue3.pop();
        index++;
    }
    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;
    std::deque<Cell> deque3(6);
    std::generate(deque3.begin(), deque3.end(), generate);
    std::cout << "deque3:   ";
    std::copy(deque3.begin(), deque3.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;

    std::queue<Cell> queue4(deque2);
    std::queue<Cell> queue5(deque3);
    std::cout << "swap before:  " << std::endl;
    queuePrint("queue4:   ", queue4);
    queuePrint("queue5:   ", queue5);
    //交换容器适配器与 other 的内容。
    //等效地调用 using std::swap; swap(c, other.c);
    queue4.swap(queue5);
    std::cout << "swap after:  " << std::endl;
    queuePrint("queue4:   ", queue4);
    queuePrint("queue5:   ", queue5);
}

输出

 


http://lihuaxi.xjx100.cn/news/1049382.html

相关文章

ObjectARX中的坐标系以及坐标转换

1 AutoCAD中的坐标系种类 WCS World Coordinate System. The “reference” coordinate system. All other coordinate systems are defined relative to the WCS, which never changes. Values measured relative to the WCS are stable across changes to other coordinate s…

从 Dev 和 Ops 视角出发,聊聊 DevSecOps 的 What / Why / How

近日&#xff0c;极小狐和 TA 的朋友们相聚上海&#xff0c;开展了一场技术 Meetup&#xff0c;从 DevSecOps 的 What、Why、How 出发&#xff0c;通过分享真实应用案例&#xff0c;与参会者交流 DevSecOps 的实践过程和落地经验。 本文整理自极狐(GitLab) 资深云原生架构师郭旭…

flask中使用jsonify和json.dumps的区别

flask提供了jsonify函数供用户处理返回的序列化json数据,而python自带的json库中也有dumps方法可以序列化json对象,那么在flask的视图函数中return它们会有什么不同之处呢? 想必开始很多人和我一样搞不清楚,只知道既然框架提供了方法就用,肯定不会错。 但作为开发人员,…

Linux学习记录——이십 进程间通信(2)共享内存

文章目录 1、system V共享内存1、原理2、模拟实现关联共享内存 3.共享内存大小4、共享内存特点 1、system V共享内存 system是一套标准&#xff0c;独立于文件系统之外&#xff0c;是系统专门为通信而设计出来的内核模块&#xff0c;称之为system V的IPC通信机制。 共享内存的…

[java]String类

String表示字符串类型。 注意c中没有表示字符串的类型。 String内部包含如下两个变量。 java中String结尾没有/0&#xff0c;java不需要/0标注结束位置。 str2代表指向的对象内容为空&#xff0c;str3代表不指向任何对象。 str1和str2指向对象不一样&#xff0c;所以不相等 可…

机器学习——TensorFlow2

1.创建矩阵 1. 全零矩阵&#xff1a;tf.zeros(shape, dtypeNone, nameNone) - shape参数指定了矩阵的形状&#xff0c;可以是一个整数列表或元组。 - dtype参数指定了生成矩阵的数据类型&#xff0c;默认为tf.float32。 2. 全1矩阵&#xff1a;tf.ones(shape, dtypeNone, name…

基于matlab使用 CSI-RS 的 NR 下行链路发射端波束细化

一、前言 此示例演示了使用 5G 工具箱中的信道状态信息参考信号 &#xff08;CSI-RS&#xff09; 的下行链路发射端波束细化过程。该示例展示了如何在散射环境中向不同方向传输多个CSI-RS资源&#xff0c;以及如何根据参考信号接收功率&#xff08;RSRP&#xff09;测量结果选择…

( “树” 之 DFS) 337. 打家劫舍 III ——【Leetcode每日一题】

337. 打家劫舍 III 小偷又发现了一个新的可行窃的地区。这个地区只有一个入口&#xff0c;我们称之为 root。 除了 root 之外&#xff0c;每栋房子有且只有一个“父“房子与之相连。一番侦察之后&#xff0c;聪明的小偷意识到“这个地方的所有房屋的排列类似于一棵二叉树”。…