Namespaces
Variants

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

来自 zh.cppreference.net

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 InputIt >
queue( InputIt first, InputIt last );
(6) (自 C++23 起)
template< class Alloc >
explicit queue( const Alloc& alloc );
(7) (自 C++11 起)
template< class Alloc >
queue( const Container& cont, const Alloc& alloc );
(8) (自 C++11 起)
template< class Alloc >
queue( Container&& cont, const Alloc& alloc );
(9) (自 C++11 起)
template< class Alloc >
queue( const queue& other, const Alloc& alloc );
(10) (自 C++11 起)
template< class Alloc >
queue( queue&& other, const Alloc& alloc );
(11) (自 C++11 起)
template< class InputIt, class Alloc >
queue( InputIt first, InputIt last, const Alloc& alloc );
(12) (自 C++23 起)
template< container-compatible-range<T> R>
queue( std::from_range_t, R&& rg );
(13) (自 C++23 起)
template< container-compatible-range<T> R, class Alloc >
queue( std::from_range_t, R&& rg, const Alloc& alloc );
(14) (自 C++23 起)

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

1) 默认构造函数。对容器进行值初始化。
2) 复制构造底层容器 c,其内容为 cont。 这也是默认构造函数。(直到 C++11)
3) 移动构造底层容器 c,其内容为 std::move(cont)。
4) 复制构造函数。适配器以 other.c 的内容复制构造。
5) 移动构造函数。适配器以 std::move(other.c) 构造。
6) 构造底层容器 c,其内容为范围 [first, last)。仅当 InputIt 满足 LegacyInputIterator 时,此重载参与重载决议。
7-12) 仅当 std::uses_allocator<Container, Alloc>::value 为 true 时,这些构造函数参与重载决议,即底层容器是识别分配器的容器(对于所有能与 queue 一起使用的标准库容器均为 true)。
7) 使用 alloc 作为分配器构造底层容器,如同以 c(alloc) 一样。
8) 构造底层容器,其内容为 cont 并使用 alloc 作为分配器,如同以 c(cont, alloc) 一样。
9) 构造底层容器,其内容为 cont(使用移动语义)同时使用 alloc 作为分配器,如同以 c(std::move(cont), alloc) 一样。
10) 构造适配器,其内容为 other.c 并使用 alloc 作为分配器,如同以 c(other.c, alloc) 一样。
11) 构造适配器,其内容为 other(使用移动语义)同时使用 alloc 作为分配器,如同以 c(std::move(other.c), alloc) 一样。
12) 构造底层容器,其内容为范围 [first, last) 并使用 alloc 作为分配器,如同以 c(first, last, alloc) 一样。仅当 InputIt 满足 LegacyInputIterator 时,此重载参与重载决议。
13) 构造底层容器,其内容为 ranges::to<Container>(std::forward<R>(rg))。
14) 构造底层容器,其内容为 ranges::to<Container>(std::forward<R>(rg), alloc)。

参数

alloc - 用于底层容器所有内存分配的分配器
other - 用作初始化底层容器来源的另一容器适配器
cont - 用作初始化底层容器来源的容器
first, last - 定义待初始化元素源 范围 的迭代器对
rg - 容器兼容范围 ,即元素可转换为 T 的 input_range
类型要求
-
Alloc 必须满足 Allocator 要求
-
Container 必须满足 Container 要求。仅当 Container 满足 AllocatorAwareContainer 要求时,接受分配器参数的构造函数才会参与重载决议
-
InputIt 必须满足 LegacyInputIterator 要求

复杂度

与所包装容器上的相应操作相同。

注释

功能测试 宏 值 标准 功能特性
__cpp_lib_adaptor_iterator_pair_constructor 202106L (C++23) std::queue 和 std::stack 的迭代器对构造函数;重载版本 ( 6 ) 和 ( 12 )
__cpp_lib_containers_ranges 202202L (C++23) 范围感知 的构造与插入操作;重载版本 ( 13 ) 和 ( 14 )

示例

#include <cassert>
#include <deque>
#include <iostream>
#include <memory>
#include <ranges>
#include <queue>
int main()
{
    std::queue<int> c1;
    c1.push(5);
    assert(c1.size() == 1);
    std::queue<int> c2(c1);
    assert(c2.size() == 1);
    std::deque<int> deq{3, 1, 4, 1, 5};
    std::queue<int> c3(deq); // 重载 (2)
    assert(c3.size() == 5);
# ifdef __cpp_lib_adaptor_iterator_pair_constructor
    const auto il = {2, 7, 1, 8, 2};
    std::queue<int> c4{il.begin(), il.end()}; // C++23, (6)
    assert(c4.size() == 5);
# endif
# if __cpp_lib_containers_ranges >= 202202L
    // C++23, 重载 (13)
    auto c5 = std::queue(std::from_range_t, std::ranges::iota(0, 42));
    assert(c5.size() == 42);
    // 使用管道语法实现相同效果,内部使用重载 (13)
    auto c6 = std::ranges::iota(0, 42) | std::ranges::to<std::queue>();
    assert(c6.size() == 42);
    std::allocator<int> alloc;
    // C++23, 重载 (14)
    auto c7 = std::queue(std::from_range_t, std::ranges::iota(0, 42), alloc);
    assert(c7.size() == 42);
    // 使用管道语法实现相同效果,内部使用重载 (14)
    auto c8 = std::ranges::iota(0, 42) | std::ranges::to<std::queue>(alloc);
    assert(c8.size() == 42);
# endif
}

缺陷报告

以下行为变更缺陷报告被追溯应用于先前发布的C++标准。

缺陷报告 适用范围 发布时的行为 正确行为
P0935R0 C++11 默认构造函数为显式 改为隐式

另请参阅

为容器适配器赋值
(公开成员函数)