std::
deque::deque
公众成员函数 <deque>
C++98
C++11
C++14
C++98:
default (1)
explicit deque (const allocator_type& alloc = allocator_type());
fill (2)
explicit deque (size_type n, const value_type& val = value_type(),
const allocator_type& alloc = allocator_type());
range (3)
template <class InputIterator>
deque (InputIterator first, InputIterator last,
const allocator_type& alloc = allocator_type());
copy (4)
deque (const deque& x);
C++11;
default (1)
explicit deque (const allocator_type& alloc = allocator_type());
fill (2)
explicit deque (size_type n);
deque (size_type n, const value_type& val,
const allocator_type& alloc = allocator_type());
range (3)
template <class InputIterator>
deque (InputIterator first, InputIterator last,
const allocator_type& alloc = allocator_type());
copy (4)
deque (const deque& x);
deque (const deque& x, const allocator_type& alloc)
move (5)
deque (deque&& x);
deque (deque&& x, const allocator_type& alloc);
initializer list (6)
deque (initializer_list<value_type> il,
const allocator_type& alloc = allocator_type());
C++14:
default (1)
deque();
explicit deque (const allocator_type& alloc);
fill (2)
explicit deque (size_type n, const allocator_type& alloc = allocator_type());
deque (size_type n, const value_type& val,
const allocator_type& alloc = allocator_type());
range (3)
template <class InputIterator>
deque (InputIterator first, InputIterator last,
const allocator_type& alloc = allocator_type());
copy (4)
deque (const deque& x);
deque (const deque& x, const allocator_type& alloc);
move (5)
deque (deque&& x);
deque (deque&& x, const allocator_type& alloc);
initializer list (6)
deque (initializer_list<value_type> il,
const allocator_type& alloc = allocator_type());
构建双端队列容器
构造一个deque容器对象,根据使用的构造函数版本初始化其内容:
C++98
C++11
C++98:
(1)空容器构造函数(默认构造函数)
构造一个空容器,没有元素。
(2)默认填充构造函数
构造一个包含n个元素的容器。每个元素都是val的副本。
(3)范围构造函数
构造与范围(first,last)相同数量的元素的容器,每个元素都由该范围中相应的元素以相同的顺序构造.
(4)拷贝构造函数
使用x中每个元素的副本以相同的顺序构造容器。
容器保留了alloc的内部副本,在整个生命周期中用于分配存储空间。
如果没有alloc参数传递给构造函数,则使用默认构造的分配器,以下情况除外:
-拷贝构造函数(4)创建一个容器,保存并使用x分配器的副本。
元素的存储是使用这个内部分配器分配的。
C++11:
(1)空容器构造函数(默认构造函数)
构造一个空容器,没有元素。
(2)默认填充构造函数
构造一个包含n个元素的容器。每个元素都是val的副本。
(3)范围构造函数
构造与范围(first,last)相同数量的元素的容器,每个元素都由该范围中相应的元素以相同的顺序构造.
(4)拷贝构造函数(和使用分配器复制)
使用x中每个元素的副本以相同的顺序构造容器。
(5)移动构造函数(和使用分配器移动)
构造一个获取x元素的容器。
如果指定了alloc并且与x的分配器不同,则移动元素。
否则,不构造元素(所有权直接转移)。
X处于未指定但有效的状态。
(6)初始化列表构造函数
使用il中每个元素的副本以相同的顺序构造容器。
容器保存了一个alloc的内部副本,用于分配和释放元素的存储空间,
以及构造和销毁它们(由它的allocator_traits指定)。如果没有alloc参数传递给构造函数,
则使用默认构造的分配器,以下情况除外:
- 复制构造函数(4,第一个类型)创建一个容器,
保存并使用通过调用x的分配器上相应的selected_on_container_copy_construction特性返回的分配器的副本。
- 移动构造函数(5,第一个类型)获得x的分配器。
通过使用相应的参数调用allocator_traits::construct,可以复制、移动所有元素,或者以其他方式构造所有元素。
☲ 参数
alloc
分配器对象。
容器保存并使用这个分配器的内部副本.
成员类型allocator_type是容器使用的内部分配器类型,
在deque中定义为第二个模板形参(Alloc)的别名.
如果allocator_type是默认分配器(没有状态)的实例化,这就无关紧要了.
n
容器初始大小(即构造时容器中元素的数量)。
成员类型size_type是一个无符号整型。
val
填充容器的值.
容器中的n个元素的每一个都将被初始化为这个值的副本。
成员类型value_type是容器中元素的类型,在deque中定义为其第一个模板形参(T)的别名。
first, last
输入迭代器初始和最终位置的范围。
使用的范围是[first,last),它包括first和last之间的所有元素,
包括first所指向的元素,但不包括last所指向的元素。
函数模板实参InputIterator必须是一个输入迭代器类型,该类型指向可以构造value_type对象的类型的元素。
x
用来复制或获取的一个相同类型的 deque对象(具有相同的类模板参数T和Alloc),其内容被复制或获取。
il
一个initializer_list对象。
这些对象是由初始化列表声明符自动构造的。
成员类型value_type是容器中元素的类型,在 deque中定义为其第一个模板形参(T)的别名。
☣ 示例
// constructing deques
#include <iostream>
#include <deque>
int main ()
{
unsigned int i;
// constructors used in the same order as described above:
std::deque<int> first; // empty deque of ints
std::deque<int> second (4,100); // four ints with value 100
std::deque<int> third (second.begin(),second.end()); // iterating through second
std::deque<int> fourth (third); // a copy of third
// the iterator constructor can be used to copy arrays:
int myints[] = {16,2,77,29};
std::deque<int> fifth (myints, myints + sizeof(myints) / sizeof(int) );
std::cout << "The contents of fifth are:";
for (std::deque<int>::iterator it = fifth.begin(); it!=fifth.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
输出:
The contents of fifth are: 16 2 77 29
✥ 复杂度
常量用于默认构造函数(1)和move构造函数(5)(除非alloc与x的分配器不同)。
对于所有其他情况,结果容器大小是线性的。
☣ 迭代器的有效性
移动构造函数(5),如果元素被移动,则使所有与x相关的迭代器、指针和引用失效。
⇄ 数据竞争
访问所有复制的元素。移动构造函数(5)修改x。
☂ 异常安全性
强保证:在抛出异常的情况下没有影响。
如果allocator_traits::construct不支持元素结构的适当参数,
或者如果[first,last)指定的范围无效,它将导致未定义的行为。
🍄 另请参阅
deque::operator=
返回一个指向数组末尾元素的下一个元素的迭代器 (公众成员函数)
deque::assign
访问最后一个元素(公众成员函数)
deque::reserve
返回一个指向数组开头的常量迭代器 (公众成员函数)
vector::resize
返回指向数组最后一个元素的常量反向迭代器(公众成员函数)
vector::clear
返回指向数组开始元素前一个元素的常量反向迭代器 (公众成员函数)