C++官网参考链接:https://cplusplus.com/reference/deque/deque/
类模板
<deque>
std::deque
template < class T, class Alloc = allocator<T> > class deque;
双端队列
deque(通常发音像"deck")是双端队列的不规则首字母缩写。双端队列是具有动态大小的顺序容器,可以在两端(前端或后端)进行扩展或收缩。
特定的库可能以不同的方式实现deque,通常是作为动态数组的某种形式。但在任何情况下,它们都允许通过随机访问iterator直接访问单个元素,并根据需要通过扩展和收缩容器自动处理存储。
因此,它们提供了类似于vectors的功能,但可以在序列的开始高效地插入和删除元素,而不仅仅是在序列的结束。但是,与vectors不同,deques不能保证将其所有元素存储在连续的存储位置:通过偏移指向另一个元素的指针来访问deque中的元素会导致未定义的行为。
vectors和deque都提供了非常相似的接口,也可以用于类似的目的,但在内部它们的工作方式却截然不同:vector使用一个单独的数组,需要偶尔重新分配以实现增长,而deque的元素可以分散在不同的存储块中,容器在内部保存必要的信息,以便在恒定的时间内通过统一的顺序接口(通过iterator)直接访问其任何元素。因此,deque在内部比vectors更复杂一些,但这允许它们在某些情况下更有效地增长,特别是对于非常长的序列,在这种情况下重新分配会变得更加昂贵。
对于包含频繁插入或删除除开始或结束位置以外的元素的操作,deque的性能较差,iterator和reference的一致性不如lists和forward lists。
容器的属性
按顺序排列
顺序容器中的元素按照严格的线性顺序排列。各个元素通过它们在该序列中的位置进行访问。
动态数组
它通常作为动态数组实现,允许直接访问序列中的任何元素,并在序列的开始或结束提供相对快速的添加/删除元素。
能够感知的allocator
容器使用allocator对象动态处理其存储需求。
模板形参
T
元素的类型。
别名为成员类型deque::value_type。
Alloc
用于定义存储分配模型的allocator对象的类型。默认情况下,使用allocator类模板,它定义了最简单的内存分配模型,并且与值无关。
别名为成员类型deque::allocator_type。
成员类型
C++98
member type | definition | notes |
---|---|---|
value_type | The first template parameter (T) | |
allocator_type | The second template parameter (Alloc) | defaults to: allocator<value_type> |
reference | allocator_type::reference | for the default allocator: value_type& |
const_reference | allocator_type::const_reference | for the default allocator: const value_type& |
pointer | allocator_type::pointer | for the default allocator: value_type* |
const_pointer | allocator_type::const_pointer | for the default allocator: const value_type* |
iterator | a random access iterator to value_type | convertible to const_iterator |
const_iterator | a random access iterator to const value_type | |
reverse_iterator | reverse_iterator<iterator> | |
const_reverse_iterator | reverse_iterator<const_iterator> | |
difference_type | a signed integral type, identical to: iterator_traits<iterator>::difference_type | usually the same as ptrdiff_t |
size_type | an unsigned integral type that can represent any non-negative value of difference_type | usually the same as size_t |
C++11
member type | definition | notes |
---|---|---|
value_type | The first template parameter (T) | |
allocator_type | The second template parameter (Alloc) | defaults to: allocator<value_type> |
reference | value_type& | |
const_reference | const value_type& | |
pointer | allocator_traits<allocator_type>::pointer | for the default allocator: value_type* |
const_pointer | allocator_traits<allocator_type>::const_pointer | for the default allocator: const value_type* |
iterator | a random access iterator to value_type | convertible to const_iterator |
const_iterator | a random access iterator to const value_type | |
reverse_iterator | reverse_iterator<iterator> | |
const_reverse_iterator | reverse_iterator<const_iterator> | |
difference_type | a signed integral type, identical to: iterator_traits<iterator>::difference_type | usually the same as ptrdiff_t |
size_type | an unsigned integral type that can represent any non-negative value of difference_type | usually the same as size_t |
成员函数
(constructor) Construct deque container (public member function)
(destructor) Deque destructor (public member function)
operator= Assign content (public member function)
iterator:
begin Return iterator to beginning (public member function)
end Return iterator to end (public member function)
rbegin Return reverse iterator to reverse beginning (public member function)
rend Return reverse iterator to reverse end (public member function)
cbegin Return const_iterator to beginning (public member function)
cend Return const_iterator to end (public member function)
crbegin Return const_reverse_iterator to reverse beginning (public member function)
crend Return const_reverse_iterator to reverse end (public member function)
容量:
size Return size (public member function)
max_size Return maximum size (public member function)
resize Change size (public member function)
empty Test whether container is empty (public member function)
shrink_to_fit Shrink to fit (public member function)
元素访问:
operator[] Access element (public member function)
at Access element (public member function)
front Access first element (public member function)
back Access last element (public member function)
修改器:
assign Assign container content (public member function)
push_back Add element at the end (public member function)
push_front Insert element at beginning (public member function)
pop_back Delete last element (public member function)
pop_front Delete first element (public member function)
insert Insert elements (public member function)
erase Erase elements (public member function)
swap Swap content (public member function)
clear Clear content (public member function)
emplace Construct and insert element (public member function)
emplace_front Construct and insert element at beginning (public member function)
emplace_back Construct and insert element at the end (public member function)
allocator:
get_allocator Get allocator (public member function)
非成员函数重载:
relational operators Relational operators for deque (function)
swap Exchanges the contents of two deque containers (function template)