Namespaces
Variants

std::deque<T,Allocator>:: deque

From cppreference.net
deque ( ) : deque ( Allocator ( ) ) { }
(1) (自 C++11 起)
(自 C++26 起为 constexpr)
(2)
explicit deque ( const Allocator & alloc = Allocator ( ) ) ;
(C++11 前)
explicit deque ( const Allocator & alloc ) ;
(自 C++11 起)
(自 C++26 起为 constexpr)
explicit deque ( size_type count,
const Allocator & alloc = Allocator ( ) ) ;
(3) (自 C++11 起)
(自 C++26 起为 constexpr)
(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 起)
(自 C++26 起为 constexpr)
template < class InputIt >

deque ( InputIt first, InputIt last,

const Allocator & alloc = Allocator ( ) ) ;
(5) (自 C++26 起为 constexpr)
template < container-compatible-range < T > R >

deque ( std:: from_range_t , R && rg,

const Allocator & alloc = Allocator ( ) ) ;
(6) (自 C++23 起)
(自 C++26 起为 constexpr)
deque ( const deque & other ) ;
(7) (自 C++26 起为 constexpr)
deque ( deque && other ) ;
(8) (自 C++11 起)
(自 C++26 起为 constexpr)
(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 起)
(自 C++26 起为 constexpr)
(10)
deque ( deque && other, const Allocator & alloc ) ;
(自 C++11 起)
(C++23 前)
deque ( deque && other, const std:: type_identity_t < Allocator > & alloc ) ;
(自 C++23 起)
(自 C++26 起为 constexpr)
deque ( std:: initializer_list < T > init,
const Allocator & alloc = Allocator ( ) ) ;
(11) (自 C++11 起)
(自 C++26 起为 constexpr)

从多种数据源构造新的 deque ,可选择使用用户提供的分配器 alloc

1) 自 C++11 起可用的默认构造函数。构造一个空的 deque ,使用默认构造的分配器。
如果 Allocator 不是 可默认构造的 ,则行为未定义。
2) C++11 之前的默认构造函数。使用给定的分配器 alloc 构造一个空的 deque
3) 构造一个包含 count 个默认插入的 T 类型对象的 deque 。不进行任何拷贝操作。
如果 T 不是 DefaultInsertable deque 中,则行为未定义。
4) 构造一个包含 count 个元素副本的 deque ,每个元素的值均为 value

T 不可 复制插入 deque 中,则行为未定义。

(C++11 起)
5) 构造一个包含范围 [ first , last ) 内容的 deque 。范围 [ first , last ) 中的每个迭代器都会被精确解引用一次。

如果 InputIt 不满足 LegacyInputIterator 的要求,则会改为调用重载 (4) ,参数为 static_cast < size_type > ( first ) last alloc

(C++11 前)

仅当 InputIt 满足 LegacyInputIterator 的要求时,此重载才会参与重载决议。

如果 T 无法通过 * first 进行 EmplaceConstructible deque 中,则行为未定义。

(C++11 起)
6) 构造一个包含范围 rg 内容的 deque rg 中的每个迭代器都会被精确解引用一次。
T 不可从 * ranges:: begin ( rg ) 置入构造到 deque 中,则行为未定义。
7) 复制构造函数。构造一个包含 other 内容的 deque

分配器的获取方式类似于调用
std:: allocator_traits < Allocator > :: select_on_container_copy_construction
( other. get_allocator ( ) )

(since C++11)
8) 移动构造函数。构造一个包含 other 内容的 deque 。分配器通过从 other. get_allocator ( ) 进行移动构造获得。
9) 与复制构造函数相同,区别在于使用 alloc 作为分配器。
如果 T 不满足 CopyInsertable deque 的要求,则行为未定义。
10) 与移动构造函数相同,不同之处在于使用 alloc 作为分配器。
如果 T 不满足 MoveInsertable deque 的要求,则行为未定义。
11) 等价于 deque ( il. begin ( ) , il. end ( ) , alloc )

目录

参数

alloc - 用于此容器所有内存分配的分配器
count - 容器的大小
value - 用于初始化容器元素的值
first, last - 定义要复制的元素源 范围 的迭代器对
other - 用作源容器以初始化当前容器元素的另一容器
init - 用于初始化容器元素的初始化列表
rg - 容器兼容范围

复杂度

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 ) ( 10 ) ),指向 other 的引用、指针和迭代器(除尾后迭代器外)仍然有效,但会引用现在位于 * this 中的元素。当前标准通过 [container.reqmts]/67 中的总体声明提供此保证,并且正在通过 LWG issue 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 仅当 InputIt 是整型时
重载 ( 5 ) 才会调用重载 ( 4 )
InputIt 不是
LegacyInputIterator 时调用重载 ( 4 )
LWG 2193 C++11 默认构造函数为 explicit 改为非 explicit
LWG 2210 C++11 重载 ( 3 ) 没有分配器参数 添加该参数
N3346 C++11 对于重载 ( 3 ) ,容器中的
元素进行值初始化
改为默认插入

参见

为容器赋值
(公开成员函数)
为容器赋值
(公开成员函数)