std::
queue::queue
公众成员函数 <queue>
C++98; |
explicit queue (const container_type& ctnr = container_type()); |
C++11; |
initialize (1) |
explicit queue (const container_type& ctnr); |
|
move-initialize (2) |
explicit queue (container_type&& ctnr = container_type()); |
|
allocator (3) |
template <class Alloc> explicit queue (const Alloc& alloc); |
|
init + allocator (4) |
template <class Alloc> queue (const container_type& ctnr, const Alloc& alloc); |
|
move-init + allocator (5) |
template <class Alloc> queue (container_type&& ctnr, const Alloc& alloc); |
|
copy + allocator (6) |
template <class Alloc> queue (const queue& x, const Alloc& alloc); |
|
move + allocator (7) |
template <class Alloc> queue (queue&& x, const Alloc& alloc); |
构建队列
构造一个队列容器适配器对象:
C++98:
-
容器适配器在内部将容器对象保存为数据。
该容器对象是传递给构造函数的ctnr参数的副本(如果有的话),否则它是一个空容器。
C++11:
容器适配器在内部将容器对象保存为data,由构造函数初始化:
-
(1)初始化构造函数
-
构造一个容器适配器,其内部容器初始化为ctnr的副本。
-
(2)移动初始化构造函数
-
构造一个容器适配器,它的内部容器通过移动构造来获取ctnr的值。
-
(3)分配器构造函数
-
构造一个容器适配器,其内部容器使用alloc作为参数构造。
-
(4)使用分配器构造函数进行初始化
-
构造一个容器适配器,其内部容器使用cntr和alloc作为参数构造。
-
(5)使用分配器构造函数移动初始化
-
构造一个容器适配器,其内部容器使用std::move(cntr)和alloc作为参数构造。
-
(6)使用分配器构造函数复制
-
构造一个容器适配器,它的内部容器以x的内部容器作为第一个参数,alloc作为第二个参数。
-
(7)使用分配器构造函数移动
-
构造一个容器适配器,它的内部容器是通过移动x的内部容器作为第一个参数并传递alloc作为第二个参数来构造的。
☲ 参数
-
ctnr
-
容器对象。
container_type是底层容器类型的类型(定义为第二个类模板形参container的别名;见成员类型)。
-
alloc
-
分配器对象。
Alloc必须是uses_allocator::value为true的类型(对于其他类型,构造函数甚至不参与重载解析)。
-
x
-
相同类型的队列(即具有相同模板参数,T和Container)。
☣ 示例
// constructing queues
#include <iostream> // std::cout
#include <deque> // std::deque
#include <list> // std::list
#include <queue> // std::queue
int main ()
{
std::deque<int> mydeck (3,100); // deque with 3 elements
std::list<int> mylist (2,200); // list with 2 elements
std::queue<int> first; // empty queue
std::queue<int> second (mydeck); // queue initialized to copy of deque
std::queue<int,std::list<int> > third; // empty queue with list as underlying container
std::queue<int,std::list<int> > fourth (mylist);
std::cout << "size of first: " << first.size() << '\n';
std::cout << "size of second: " << second.size() << '\n';
std::cout << "size of third: " << third.size() << '\n';
std::cout << "size of fourth: " << fourth.size() << '\n';
return 0;
} |
输出:
size of first: 0
size of second: 3
size of third: 0
size of fourth: 2
✥ 复杂度
一个容器构造(对于标准容器而言,其大小达到线性)。
☣ 迭代器的有效性
移动构造函数(2)和(7)可能使所有与moved实参相关的迭代器、指针和引用失效。
⇄ 数据竞争
访问所有复制的元素。动构造函数(2)和(7)可能修改它们的(第一个)参数。
☂ 异常安全性
提供与在容器上执行的操作相同级别的保证。
🍄 另请参阅