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

来自cppreference.com
< cpp‎ | container‎ | queue

queue() : queue(Container()) { }
(1) (C++11 起)
(2)
explicit queue( const Container& cont = Container() );
(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 起)

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

1) 默认构造函数。值初始化容器。
2)cont 的内容复制构造底层容器 c此亦为默认构造函数。 (C++11 前)
3)std::move(cont) 移动构造底层容器 c
4) 复制构造函数。以 other.c 的内容复制构造适配器。
5) 移动构造函数。以 std::move(other.c) 构造适配器。
6) 以范围 [first, last) 的内容构造底层容器 c 。此重载仅若 InputIt 满足老式输入迭代器 (LegacyInputIterator) 才参与重载决议。
7-12) 这些构造函数仅若 std::uses_allocator<Container, Alloc>true ,即底层容器是知分配器容器(对所有标准库容器为 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) 才参与重载决议。

参数

alloc - 用于底层容器所有内存分配的分配器
other - 用作源初始化底层容器的另一容器适配器
cont - 用作源初始化底层容器的容器
first, last - 用以初始化的元素范围
类型要求
-
Alloc 必须满足分配器 (Allocator) 的要求。
-
Container 必须满足容器 (Container) 的要求。接受一个分配器参数的构造函数仅若 Container 满足知分配器容器 (AllocatorAwareContainer) 的要求参与重载决议。
-
InputIt 必须满足老式输入迭代器 (LegacyInputIterator) 的要求。

复杂度

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

示例

#include <queue>
#include <deque>
#include <iostream>
 
int main()
{
    std::queue<int> c1;
    c1.push(5);
    std::cout << c1.size() << '\n';
 
    std::queue<int> c2(c1);
    std::cout << c2.size() << '\n';
 
    std::deque<int> deq {3, 1, 4, 1, 5};
    std::queue<int> c3(deq);
    std::cout << c3.size() << '\n';
}

输出:

1
1
5

缺陷报告

下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。

DR 应用于 出版时的行为 正确行为
P0935R0 C++11 默认构造函数曾为 explicit 使之为隐式

参阅

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