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

定义于头文件 <queue>
template<

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

> class queue;

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

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

成员函数

构造 queue

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

queue() : queue(Container()) { }

(1) (C++11 起)

explicit queue( const Container& cont = Container() );

(2) (C++11 前)

explicit queue( const Container& cont );

(C++11 起)

explicit queue( Container&& cont );

(3) (C++11 起)

queue( const queue& other );

(4)

queue( queue&& other );

(5) (C++11 起)

template< class Alloc >
explicit queue( const Alloc& alloc );

(6) (C++11 起)

template< class Alloc >
queue( const Container& cont, const Alloc& alloc );

(7) (C++11 起)

template< class Alloc >
queue( Container&& cont, const Alloc& alloc );

(8) (C++11 起)

template< class Alloc >
queue( const queue& other, const Alloc& alloc );

(9) (C++11 起)

template< class Alloc >
queue( queue&& other, const Alloc& alloc );

(10) (C++11 起)

从各种数据源构造容器适配器的新底层容器。

1) 默认构造函数。值初始化容器。

2) 以 cont 的内容复制构造底层容器 c 。此亦为默认构造函数。 (C++11 前)

3) 以 std::move(cont) 移动构造底层容器 c

4) 复制构造函数。适配器以 other.c 的内容复制构造。(隐式声明)

5) 移动构造函数。适配器以 std::move(other.c) 构造。(隐式声明)

6-10) 仅若 std::uses_allocator<container_type, Alloc>::value == true ,即底层容器是具分配器容器(对所有标准库容器为 true )才定义下列构造函数。

6) 以 alloc 为分配器构造底层容器,如同以 c(alloc) 。

7) 用 cont 的内容,并以 alloc 为分配器构造底层容器,如同以 c(cont, alloc) 。

8) 以 cont 的内容用移动语义,同时以 alloc 为分配器构造底层容器,如同以 c(std::move(cont), alloc) 。

9) 以 other.c 的内容,并以 alloc 为分配器构造适配器,如同以 c(other.c, alloc) 。

10) 以 other 的内容使用移动语义,并以 alloc 为分配器构造适配器,如同以 c(std::move(other.c), alloc) 。

参数

alloc - 用于底层容器所有内存分配的分配器
other - 用作源初始化底层容器的另一容器适配器
cont - 用作源初始化底层容器的容器
first, last - 用以初始化的元素
类型要求
- Alloc 必须满足分配器 (Allocator) 的要求。
- Container 必须满足容器 (Container) 的要求。仅若 Container 满足具分配器容器 (AllocatorAwareContainer) 的要求才定义构造函数 (5-10)
- InputIt 必须满足遗留输入迭代器 (LegacyInputIterator) 的要求。

复杂度

同被包装容器上的对应操作。

析构 queue

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

~queue();

销毁容器适配器。调用元素的析构函数,然后解分配所用的存储。注意,若元素是指针,则不销毁所指向的对象。

复杂度

与容器适配器大小成线性。

调用示例

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

    //1) 默认构造函数。值初始化容器。
    std::queue<Cell> queue1;
    std::cout << "queue1 empty: " << queue1.empty() << std::endl;
    std::cout << std::endl;

    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> queue2(deque1);
    queuePrint("queue2:   ", queue2);
    std::cout << std::endl;

    //3) 以 std::move(cont) 移动构造底层容器 c 。
    std::queue<Cell> queue3(std::move(deque1));
    queuePrint("queue3:   ", queue3);
    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::queue<Cell> queue4(deque2);
    //4) 复制构造函数。适配器以 other.c 的内容复制构造。
    std::queue<Cell> queue5(queue4);
    queuePrint("queue5:   ", queue5);
    std::cout << std::endl;

    //5) 移动构造函数。适配器以 std::move(other.c) 构造。
    std::queue<Cell> queue6(std::move(queue4));
    queuePrint("queue6:   ", queue6);
    std::cout << "queue4 empty: " << queue4.empty() << std::endl;
}

输出

猜你喜欢

转载自blog.csdn.net/qq_40788199/article/details/130181116
今日推荐