命名空间
变体
操作

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

来自 cppreference.cn
< cpp‎ | container‎ | queue
queue() : queue(Container()) {}
(1) (since C++11)
(2)
explicit queue( const Container& cont = Container() );
(until C++11)
explicit queue( const Container& cont );
(since C++11)
explicit queue( Container&& cont );
(3) (since C++11)
queue( const queue& other );
(4) (implicitly declared)
queue( queue&& other );
(5) (since C++11)
(implicitly declared)
template< class InputIt >
queue( InputIt first, InputIt last );
(6) (since C++23)
template< class Alloc >
explicit queue( const Alloc& alloc );
(7) (since C++11)
template< class Alloc >
queue( const Container& cont, const Alloc& alloc );
(8) (since C++11)
template< class Alloc >
queue( Container&& cont, const Alloc& alloc );
(9) (since C++11)
template< class Alloc >
queue( const queue& other, const Alloc& alloc );
(10) (since C++11)
template< class Alloc >
queue( queue&& other, const Alloc& alloc );
(11) (since C++11)
template< class InputIt, class Alloc >
queue( InputIt first, InputIt last, const Alloc& alloc );
(12) (since C++23)
template< container-compatible-range<T> R>
queue( std::from_range_t, R&& rg );
(13) (since C++23)
template< container-compatible-range<T> R, class Alloc >
queue( std::from_range_t, R&& rg, const Alloc& alloc );
(14) (since C++23)

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

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

内容

[edit] 参数

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

[edit] 复杂度

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

[edit] 注解

特性测试 Std 特性
__cpp_lib_adaptor_iterator_pair_constructor 202106L (C++23) 用于 std::queuestd::stack 的迭代器对构造函数;重载 (6)(12)
__cpp_lib_containers_ranges 202202L (C++23) 范围感知构造和插入;重载 (13)(14)

[edit] 示例

#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); // overload (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, overload (13)
    auto c5 = std::queue(std::from_range_t, std::ranges::iota(0, 42));
    assert(c5.size() == 42);
 
    // the same effect with pipe syntax, internally uses overload (13)
    auto c6 = std::ranges::iota(0, 42) | std::ranges::to<std::queue>();
    assert(c6.size() == 42);
 
    std::allocator<int> alloc;
 
    // C++23, overload (14)
    auto c7 = std::queue(std::from_range_t, std::ranges::iota(0, 42), alloc);
    assert(c7.size() == 42);
 
    // the same effect with pipe syntax, internally uses overload (14)
    auto c8 = std::ranges::iota(0, 42) | std::ranges::to<std::queue>(alloc);
    assert(c8.size() == 42);
# endif
}

[edit] 缺陷报告

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

DR 应用于 已发布行为 正确行为
P0935R0 C++11 默认构造函数是显式的 改为隐式的

[edit] 参见

为容器适配器赋值
(public member function) [编辑]