std::deque<T,Allocator>::deque
来自cppreference.com
                    
                                        
                    
                    
                                                            
                    
|   deque();  | 
(1) | |
|   explicit deque( const Allocator& alloc );  | 
(2) | |
| (3) | ||
|   explicit deque( size_type count,                  const T& value = T(),  | 
(C++11 前) | |
|   deque( size_type count,                  const T& value,  | 
(C++11 起) | |
| (4) | ||
|   explicit deque( size_type count );  | 
 (C++11 起)  (C++14 前)  | 
|
|   explicit deque( size_type count, const Allocator& alloc = Allocator() );  | 
(C++14 起) | |
|   template< class InputIt > deque( InputIt first, InputIt last,   | 
(5) | |
|   deque( const deque& other );  | 
(6) | |
|   deque( const deque& other, const Allocator& alloc );  | 
(7) | (C++11 起) | 
|   deque( deque&& other );  | 
(8) | (C++11 起) | 
|   deque( deque&& other, const Allocator& alloc );  | 
(9) | (C++11 起) | 
|   deque( std::initializer_list<T> init,  const Allocator& alloc = Allocator() );  | 
(10) | (C++11 起) | 
|   template< container-compatible-range<T> R > deque( std::from_range_t, R&& rg,  | 
(11) | (C++23 起) | 
从各种数据源构造新容器,可以使用用户提供的分配器 alloc。
1) 默认构造函数。构造拥有默认构造的分配器的空容器。
2) 构造拥有给定分配器 alloc 的空容器。
3) 构造拥有 count 个有值 value 的元素的容器。
5) 构造拥有范围 
[first, last) 内容的容器。
 | 
 如果   | 
(C++11 前) | 
| 
 此重载只有在  | 
(C++11 起) | 
6) 复制构造函数。构造拥有 other 内容的容器。
 
| 
 如同通过调用  | 
(C++11 起) | 
8) 移动构造函数。用移动语义构造拥有 other 内容的容器。分配器通过属于 other 的分配器移动构造获得。
9) 有分配器扩展的移动构造函数。以 alloc 为新容器的分配器,从 other 移动内容;如果 alloc != other.get_allocator() ,那么它会导致逐元素移动。
 
| 
 在进行类模板实参推导时,只会从首个实参推导模板形参   | 
(C++23 起) | 
10) 构造拥有初始化器列表 init 内容的容器。
11) 构造拥有范围 rg 内容的容器。
参数
| alloc | - | 用于此容器所有内存分配的分配器 | 
| count | - | 容器的大小 | 
| value | - | 以之初始化容器元素的值 | 
| first, last | - | 复制元素的来源范围 | 
| other | - | 用作初始化容器元素来源的另一容器 | 
| init | - | 用作初始化元素来源的初始化器列表 | 
| rg | - |  与容器兼容的范围,即元素可以转换到 T 的 input_range
 | 
复杂度
1,2) 常数
3,4) 与 count 成线性
5) 与 first 和 last 的距离成线性
6,7) 与 other 的大小成线性
8) 常数。
9) alloc != other.get_allocator() 时是线性,否则是常数。
10) 与 init 的大小成线性。
11) 与 ranges::distance(rg) 成线性。
异常
调用 Allocator::allocate 可能抛出。
注解
在容器移动构造(重载 (8))后,指向 other 的引用及迭代器(除了尾迭代器)保持合法,但指代现于 *this 中的元素。当前标准由 [container.requirements.general]/12 中的总括陈述作出此保证,而 LWG 问题 2321 正在考虑更严格的保证。
示例
运行此代码
#include <deque> #include <string> #include <iostream> template<typename T> std::ostream& operator<<(std::ostream& s, const std::deque<T>& v) { s.put('['); char comma[3] = {'\0', ' ', '\0'}; for (const auto& e : v) { s << comma << e; comma[0] = ','; } return s << ']'; } int main() { // C++11 初始化器列表语法: std::deque<std::string> words1 {"the", "frogurt", "is", "also", "cursed"}; std::cout << "words1: " << words1 << '\n'; // words2 == words1 std::deque<std::string> words2(words1.begin(), words1.end()); std::cout << "words2: " << words2 << '\n'; // words3 == words1 std::deque<std::string> words3(words1); std::cout << "words3: " << words3 << '\n'; // words4 是 {"Mo", "Mo", "Mo", "Mo", "Mo"} std::deque<std::string> words4(5, "Mo"); std::cout << "words4: " << words4 << '\n'; }
输出:
words1: [the, frogurt, is, also, cursed] words2: [the, frogurt, is, also, cursed] words3: [the, frogurt, is, also, cursed] words4: [Mo, Mo, Mo, Mo, Mo]
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| 缺陷报告 | 应用于 | 出版时的行为 | 正确行为 | 
|---|---|---|---|
| LWG 144 | C++98 | 重载 (5) 的复杂度要求与 std::vector 的对应重载一致 | 要求线性复杂度 | 
| LWG 237 | C++98 | 重载 (5) 的复杂度要求与 first - last 成线性 | 与 std::distance(first, last) 成线性 | 
| LWG 868 | C++98 | 对于重载 (4),容器中的元素会被默认构造 | 它们会被值初始化 | 
| LWG 2193 | C++11 | 默认构造函数是 explicit 的 | 它是非 explicit 的 | 
参阅
|    将值赋给容器   (公开成员函数)  | |
|    赋值给容器   (公开成员函数)  |