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