首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::deque

Defined in header <deque>

template< class T, class Allocator = std::allocator<T> > class deque;

(1)

namespace pmr { template <class T> using deque = std::deque<T, std::pmr::polymorphic_allocator<T>>; }

(2)

(since C++17)

std::deque%28双结束队列%29是一个索引序列容器,它允许在开始和结束时快速插入和删除。此外,在deque的两端插入和删除不会使指针或对其余元素的引用无效。

相对于std::vector,deque的元素不是连续存储的:典型实现使用一系列单独分配的固定大小数组,并附加簿记,这意味着索引访问std:deque必须执行两个指针取消,而向量%27s索引访问只执行一个。

根据需要,自动扩展和收缩DEQE的存储。扩展一个设备比扩展一个std::vector因为它不涉及将现有元素复制到新的内存位置。另一方面,deques通常具有很大的最小内存开销;一个只包含一个元素的deque必须分配其完整的内部数组%28e.g。64位libstdc++上的对象大小是对象大小的8倍;64位libc++%29上的对象大小是对象大小的16倍或4096字节,以较大者为准。

对deques的常见操作的复杂性%28---效率%---29如下:

  • 随机存取-常数O%281%29
  • 在结尾或开头插入或删除元素-常数O%281%29
  • 元素的插入或去除.线性O%28N%29

std::deque满足…的要求Container,,,AllocatorAwareContainer,,,SequenceContainerReversibleContainer...

模板参数

T

-

The type of the elements. T must meet the requirements of CopyAssignable and CopyConstructible. (until C++11) The requirements that are imposed on the elements depend on the actual operations performed on the container. Generally, it is required that element type is a complete type and meets the requirements of Erasable, but many member functions impose stricter requirements. (since C++11)

T must meet the requirements of CopyAssignable and CopyConstructible.

(until C++11)

The requirements that are imposed on the elements depend on the actual operations performed on the container. Generally, it is required that element type is a complete type and meets the requirements of Erasable, but many member functions impose stricter requirements.

(since C++11)

T must meet the requirements of CopyAssignable and CopyConstructible.

(until C++11)

The requirements that are imposed on the elements depend on the actual operations performed on the container. Generally, it is required that element type is a complete type and meets the requirements of Erasable, but many member functions impose stricter requirements.

(since C++11)

Allocator

-

An allocator that is used to acquire/release memory and to construct/destroy the elements in that memory. The type must meet the requirements of Allocator. The behavior is undefined if Allocator::value_type is not the same as T.

迭代器失效

本节中仍有一些不准确之处,请参阅个别成员函数页以获得更多细节。

Operations

Invalidated

All read only operations

Never

swap, std::swap

The past-the-end iterator may be invalidated (implementation defined)

shrink_to_fit, clear, insert, emplace, push_front, push_back, emplace_front, emplace_back

Always

erase

If erasing at begin - only erased elements If erasing at end - only erased elements and the past-the-end iterator Otherwise - all iterators are invalidated (including the past-the-end iterator).

resize

If the new size is smaller than the old one : only erased elements and the past-the-end iterator If the new size is bigger than the old one : all iterators are invalidated Otherwise - none iterators are invalidated.

pop_front

Only to the element erased

pop_back

Only to the element erased and the past-the-end iterator

失效笔记

  • 在deque的两端插入时,引用不会被insertemplace...
  • push_front,,,push_back,,,emplace_frontemplace_back不要使任何对deque元素的引用无效。
  • 当在deque的两端擦除时,对非擦除元素的引用不会被erase,,,pop_frontpop_back...
  • 打电话给resize使用较小的大小,不会使任何对非擦除元素的引用失效。
  • 打电话给resize使用更大的大小不会使对deque元素的引用无效。

成员类型

Member type

Definition

value_type

T

allocator_type

Allocator

size_type

Unsigned integer type (usually std::size_t)

difference_type

Signed integer type (usually std::ptrdiff_t)

reference

Allocator::reference (until C++11) value_type& (since C++11)

Allocator::reference

(until C++11)

value_type&

(since C++11)

Allocator::reference

(until C++11)

value_type&

(since C++11)

const_reference

Allocator::const_reference (until C++11) const value_type& (since C++11)

Allocator::const_reference

(until C++11)

const value_type&

(since C++11)

Allocator::const_reference

(until C++11)

const value_type&

(since C++11)

pointer

Allocator::pointer (until C++11) std::allocator_traits<Allocator>::pointer (since C++11)

Allocator::pointer

(until C++11)

std::allocator_traits<Allocator>::pointer

(since C++11)

Allocator::pointer

(until C++11)

std::allocator_traits<Allocator>::pointer

(since C++11)

const_pointer

Allocator::const_pointer (until C++11) std::allocator_traits<Allocator>::const_pointer (since C++11)

Allocator::const_pointer

(until C++11)

std::allocator_traits<Allocator>::const_pointer

(since C++11)

Allocator::const_pointer

(until C++11)

std::allocator_traits<Allocator>::const_pointer

(since C++11)

iterator

RandomAccessIterator

const_iterator

Constant random access iterator

reverse_iterator

std::reverse_iterator<iterator>

const_reverse_iterator

std::reverse_iterator<const_iterator>

成员函数

(constructor)

constructs the deque (public member function)

(destructor)

destructs the deque (public member function)

operator=

assigns values to the container (public member function)

assign

assigns values to the container (public member function)

get_allocator

returns the associated allocator (public member function)

元素存取

在访问指定元素时,使用边界检查%28公共成员函数%29

操作者。[]访问指定元素%28公共成员函数%29

前端访问第一个元素%28公共成员函数%29

返回访问最后一个元素%28公共成员函数%29

迭代器

BEGINCBEGIN将迭代器返回到开头%28的公共成员函数%29

End cend将迭代器返回到End%28公共成员函数%29

将反向迭代器返回到开头%28的公共成员函数%29

rend crend将反向迭代器返回到End%28公共成员函数%29

容量

空检查容器是否为空%28公共成员函数%29

Size返回元素数%28公共成员函数%29

马克斯[医]Size返回元素的最大可能数%28公共成员函数%29

收缩[医]到[医]FIT%28C++11%29通过释放未使用内存%28公共成员函数%29来减少内存使用

修饰符

清除内容%28公共成员功能%29

插入元素%28公共成员函数%29

嵌入%28C++11%29构造元素就地%28公共成员函数%29

擦除元素%28公共成员函数%29

推[医]向末尾%28公共成员函数%29添加一个元素

座落[医]Back%28C++11%29在%28公共成员函数%29的末尾构造一个就地元素。

波普[医]回移除最后一个元素%28公共成员函数%29

推[医]前端插入元素到开头%28公共成员函数%29

座落[医]前端%28c++11%29在开始处构造一个元素(%28公共成员函数%29)。

波普[医]前端移除第一个元素%28公共成员函数%29

调整大小更改存储的元素数%28公共成员函数%29

交换交换内容%28公共成员函数%29

非会员职能

operator==operator!=operator<operator<=operator>operator>=

lexicographically compares the values in the deque (function template)

std::swap(std::deque)

specializes the std::swap algorithm (function template)

二次

代码语言:javascript
复制
#include <iostream>
#include <deque>
 
int main()
{
    // Create a deque containing integers
    std::deque<int> d = {7, 5, 16, 8};
 
    // Add an integer to the beginning and end of the deque
    d.push_front(13);
    d.push_back(25);
 
    // Iterate and print values of deque
    for(int n : d) {
        std::cout << n << '\n';
    }
}

二次

产出:

二次

代码语言:javascript
复制
13
7
5
16
8
25

二次

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券