deque

对线性排列中给定类型的元素进行排序,并且类似于矢量,允许快速随机访问任何元素并在容器后面高效插入和删除。 但是,和矢量不同的是,deque 类还支持在容器前面高效插入和删除。

语法

template <class Type, class Allocator =allocator<Type>>
class deque

参数

Type
要在 deque 中存储的元素数据类型。

Allocator
一种表示存储的分配器对象的类型,该分配器对象封装有关 deque 的内存分配和解除分配的详细信息。 此参数是可选参数,默认值为 allocator<Type>

注解

容器类型选择通常应根据应用程序所需的搜索和插入的类型。 当对任何元素的随机访问超出限制并且仅要求在序列的末尾插入或删除元素时,Vectors 应作为用于管理序列的首选容器。 当在序列内任何位置的高效插入和删除(采用常量时间)超出限制时,list 容器的性能会很优异。 序列中间的此类操作需要元素副本和与序列中的元素数量成正比的分配(线性时间)。

当成员函数必须插入或清除序列中的元素时,将发生 Deque 的重新分配:

  • 如果元素插入到一个空序列中,或如果清除元素留下一个空序列,则之前由 beginend 返回的迭代器变为无效。

  • 如果在 deque 的第一个位置插入一个元素,则所有指定现有元素的迭代器(但没有引用)将变为无效。

  • 如果在 deque 的末尾插入一个元素,则 end 和所有指定了现有元素的迭代器(但没有引用)将变为无效。

  • 如果在 deque 的前面清除元素,只有该迭代器和对已清除元素的引用变得无效。

  • 如果从 deque 的末尾清除最后一个元素,则仅指向最后一个元素的迭代器和对清除元素的引用将变为无效。

否则,插入或清除元素将使所有迭代器和引用失效。

成员

构造函数

名称 描述
deque 构造一个 deque。 提供了多个构造函数来以不同的方式设置新 deque 的内容:空;加载了指定数量的空元素;从另一个 deque 移动或复制内容;使用迭代器复制或移动内容;以及将一个元素复制到 dequecount 次。 一些构造函数可实现使用自定义 allocator 创建元素。

Typedef

名称 描述
allocator_type 一种类型,此类型表示 allocator 对象的 deque 类。
const_iterator 一种类型,此类型提供可访问和读取 deque 中作为 const 的元素的随机访问迭代器
const_pointer 一种类型,它作为 const 提供指向 deque 中的元素的指针。
const_reference 一种类型,它作为 const 提供对 deque 中的元素的引用以供读取和其他操作。
const_reverse_iterator 一种类型,此类型提供可访问和读取 deque 中作为 const 的元素的随机访问迭代器。 以相反顺序查看 deque。 有关详细信息,请参阅 reverse_iterator
difference_type 一种类型,该类型提供引用同一 deque 中的元素的两个随机访问迭代器之间的差异。
iterator 一种类型,此类型提供可读取或修改 deque 中的任何元素的随机访问迭代器。
pointer 一种类型,它提供指向 deque 中的某个元素的指针。
reference 一种类型,此类型提供对存储在 deque 中的元素的引用。
reverse_iterator 一种类型,此类型提供可读取或修改 deque 中的某个元素的随机访问迭代器。 以相反顺序查看 deque
size_type 一种类型,此类型计算 deque 中的元素数目。
value_type 一种类型,此类型表示存储在 deque 中的数据类型。

函数

名称 描述
assign 将元素从 deque 中清除并将新的元素序列复制到目标 deque
at 返回对 deque 中指定位置的元素的引用。
back 返回对 deque 中最后一个元素的引用。
begin 返回对 deque 中第一个元素进行寻址的随机访问迭代器。
cbegin 返回指向 deque 中第一个元素的 const 迭代器。
cend 返回指向刚超出 deque 末尾位置的随机访问 const 迭代器。
clear 清除 deque 的所有元素。
crbegin 返回指向以相反顺序查看的 deque 中的第一个元素的随机访问 const 迭代器。
crend 返回指向以相反顺序查看的 deque 中的第一个元素的随机访问 const 迭代器。
emplace 将就地构造的元素插入到指定位置的 deque 中。
emplace_back 将就地构造的元素添加到 deque 的末尾。
emplace_front 将就地构造的元素添加到 deque 的开头。
empty 如果 deque 包含零个元素,则返回 true;如果它包含一个或多个元素,则返回 false
end 返回指向刚超出 deque 末尾位置的随机访问迭代器。
erase 从指定位置删除 deque 中一个或一系列元素。
front 返回对 deque 中第一个元素的引用。
get_allocator 返回用于构造 allocatordeque 对象的副本。
insert 将一个、多个或一系列元素插入到指定位置的 deque 中。
max_size 返回 deque 的最大可取长度。
pop_back 清除 deque 末尾处的元素。
pop_front 清除 deque 开头处的元素。
push_back 将元素添加到 deque 的末尾。
push_front 将元素添加到 deque 的开头。
rbegin 返回指向反向 deque 中的第一个元素的随机访问迭代器。
rend 返回指向刚超出反向 deque 中的最后一个元素位置的随机访问迭代器。
resize deque 指定新的大小。
shrink_to_fit 放弃额外容量。
size 返回 deque 中的元素数量。
swap 交换两个 deque 的元素。

运算符

名称 描述
operator[] 返回对指定位置的 deque 元素的引用。
operator= deque 的元素替换为另一个 deque 的副本。

allocator_type

一个类型,代表 deque 对象的分配器类。

typedef Allocator allocator_type;

注解

allocator_type 是模板参数 Allocator 的同义词。

示例

请参阅 get_allocator 的示例。

assign

将元素从 deque 中清除并将一组新的元素序列复制到目标 deque

template <class InputIterator>
void assign(
    InputIterator First,
    InputIterator Last);

void assign(
    size_type Count,
    const Type& Val);

void assign(initializer_list<Type> IList);

参数

First
要从参数 deque 中复制的一系列元素中的第一个元素的位置。

Last
超出要从参数 deque 中复制的一系列元素的范围的第一个元素的位置。

Count
要插入 deque 中的元素副本的数目。

Val
插入到 deque 中的元素的值。

IList
要插入到 deque 中的 initializer_list

备注

清除目标 deque 中的任何现有元素后,assign 将原始 deque 或其他 deque 中的一系列指定的元素插入目标 deque 中,或将指定值的新元素副本插入目标 deque 中。

示例

// deque_assign.cpp
// compile with: /EHsc
#include <deque>
#include <iostream>
#include <initializer_list>

int main()
{
    using namespace std;
    deque <int> c1, c2;
    deque <int>::const_iterator cIter;

    c1.push_back(10);
    c1.push_back(20);
    c1.push_back(30);
    c2.push_back(40);
    c2.push_back(50);
    c2.push_back(60);

    deque<int> d1{ 1, 2, 3, 4 };
    initializer_list<int> iList{ 5, 6, 7, 8 };
    d1.assign(iList);

    cout << "d1 = ";
    for (int i : d1)
        cout << i;
    cout << endl;

    cout << "c1 =";
    for (int i : c1)
        cout << i;
    cout << endl;

    c1.assign(++c2.begin(), c2.end());
    cout << "c1 =";
    for (int i : c1)
        cout << i;
    cout << endl;

    c1.assign(7, 4);
    cout << "c1 =";
    for (int i : c1)
        cout << i;
    cout << endl;
}
d1 = 5678c1 =102030c1 =5060c1 =4444444

at

返回对 deque 中指定位置的元素的引用。

reference at(size_type pos);

const_reference at(size_type pos) const;

参数

pos
要在 deque 中引用的元素的下标(或位置编号)。

返回值

如果 pos 大于 deque 大小,则 at 会引发异常。

注解

如果将 at 的返回值分配给 const_reference,则可以修改 deque 对象。 如果将 at 的返回值分配给 reference,则可以修改 deque 对象。

示例

// deque_at.cpp
// compile with: /EHsc
#include <deque>
#include <iostream>

int main( )
{
   using namespace std;
   deque <int> c1;

   c1.push_back( 10 );
   c1.push_back( 20 );

   const int& i = c1.at( 0 );
   int& j = c1.at( 1 );
   cout << "The first element is " << i << endl;
   cout << "The second element is " << j << endl;
}
The first element is 10
The second element is 20

back

返回对 deque 中最后一个元素的引用。

reference back();
const_reference back() const;

返回值

deque 的最后一个元素。 如果 deque 为空,则返回值不确定。

备注

如果将 back 的返回值分配给 const_reference,则可以修改 deque 对象。 如果将 back 的返回值分配给 reference,则可以修改 deque 对象。

当使用定义为 1 或 2 的 _ITERATOR_DEBUG_LEVEL 进行编译时,如果试图访问空 deque 中的元素,则将发生运行时错误。 有关更多信息,请参阅经过检查的迭代器

示例

// deque_back.cpp
// compile with: /EHsc
#include <deque>
#include <iostream>

int main( )
{
   using namespace std;
   deque <int> c1;

   c1.push_back( 10 );
   c1.push_back( 11 );

   int& i = c1.back( );
   const int& ii = c1.front( );

   cout << "The last integer of c1 is " << i << endl;
   i--;
   cout << "The next-to-last integer of c1 is " << ii << endl;
}
The last integer of c1 is 11
The next-to-last integer of c1 is 10

begin

返回一个迭代器,此迭代器用于发现 deque 中的第一个元素。

const_iterator begin() const;
iterator begin();

返回值

发现 deque 中第一个元素或空 deque 之后的位置的随机访问迭代器。

备注

如果将 begin 的返回值分配给 const_iterator,则可以修改 deque 对象。 如果将 begin 的返回值分配给 iterator,则可以修改 deque 对象。

示例

// deque_begin.cpp
// compile with: /EHsc
#include <deque>
#include <iostream>

int main( )
{
   using namespace std;
   deque <int> c1;
   deque <int>::iterator c1_Iter;
   deque <int>::const_iterator c1_cIter;

   c1.push_back( 1 );
   c1.push_back( 2 );

   c1_Iter = c1.begin( );
   cout << "The first element of c1 is " << *c1_Iter << endl;

*c1_Iter = 20;
   c1_Iter = c1.begin( );
   cout << "The first element of c1 is now " << *c1_Iter << endl;

   // The following line would be an error because iterator is const
   // *c1_cIter = 200;
}
The first element of c1 is 1
The first element of c1 is now 20

cbegin

返回确定范围中第一个元素地址的 const 迭代器。

const_iterator cbegin() const;

返回值

const 随机访问迭代器,指向范围的第一个元素,或刚超出空范围末尾的位置(对于空范围,cbegin() == cend())。

备注

由于使用 cbegin 的返回值,因此不能修改范围中的元素。

可以使用此成员函数替代 begin() 成员函数,以保证返回值为 const_iterator。 它一般与 auto 类型推导关键字联合使用,如以下示例所示。 在此示例中,将 Container 视为支持 begin()cbegin() 的可修改的任何类型的(非- const)容器。

auto i1 = Container.begin();
// i1 is Container<T>::iterator
auto i2 = Container.cbegin();

// i2 is Container<T>::const_iterator

cend

返回一个 const 迭代器,此迭代器用于发现刚超出范围中最后一个元素的位置。

const_iterator cend() const;

返回值

指向刚超出范围末尾的位置的 随机访问迭代器。

备注

cend 用于测试迭代器是否超过了其范围的末尾。

可以使用此成员函数替代 end() 成员函数,以保证返回值为 const_iterator。 它一般与 auto 类型推导关键字联合使用,如以下示例所示。 在此示例中,将 Container 视为支持 end()cend() 的可修改的任何类型的(非- const)容器。

auto i1 = Container.end();
// i1 is Container<T>::iterator
auto i2 = Container.cend();

// i2 is Container<T>::const_iterator

不应对 cend 返回的值取消引用。

clear

清除 deque 的所有元素。

void clear();

示例

// deque_clear.cpp
// compile with: /EHsc
#include <deque>
#include <iostream>

int main( )
{
   using namespace std;
   deque <int> c1;

   c1.push_back( 10 );
   c1.push_back( 20 );
   c1.push_back( 30 );

   cout << "The size of the deque is initially " << c1.size( ) << endl;
   c1.clear( );
   cout << "The size of the deque after clearing is " << c1.size( ) << endl;
}
The size of the deque is initially 3
The size of the deque after clearing is 0

const_iterator

提供可访问和读取 dequeconst 元素的随机访问迭代器的类型。

typedef implementation-defined const_iterator;

备注

const_iterator 类型不能用于修改元素的值。

示例

请参阅 back 的示例。

const_pointer

提供指向 dequeconst 元素的指针。

typedef typename Allocator::const_pointer const_pointer;

备注

const_pointer 类型不能用于修改元素的值。 iterator 更常用于访问 deque 元素。

const_reference

一种类型,此类型提供对存储在 const 中的 deque 元素的引用(用于读取和执行 const 操作)。

typedef typename Allocator::const_reference const_reference;

注解

const_reference 类型不能用于修改元素的值。

示例

// deque_const_ref.cpp
// compile with: /EHsc
#include <deque>
#include <iostream>

int main( )
{
   using namespace std;
   deque <int> c1;

   c1.push_back( 10 );
   c1.push_back( 20 );

   const deque <int> c2 = c1;
   const int &i = c2.front( );
   const int &j = c2.back( );
   cout << "The first element is " << i << endl;
   cout << "The second element is " << j << endl;

   // The following line would cause an error as c2 is const
   // c2.push_back( 30 );
}
The first element is 10
The second element is 20

const_reverse_iterator

提供可访问 deque 中任何 const 元素的随机访问迭代器的类型。

typedef std::reverse_iterator<const_iterator> const_reverse_iterator;

注解

const_reverse_iterator 类型无法修改元素的值,它用于反向循环访问 deque

示例

有关如何声明和使用 iterator 的示例,请参阅 rbegin 的示例。

crbegin

返回指向反向 deque 中第一个元素的 const 迭代器。

const_reverse_iterator crbegin() const;

返回值

发现反向 deque 中的第一个元素或发现曾是非反向 deque 中的最后一个元素的 const 反向随机访问迭代器。

备注

返回值为 crbegin 时,无法修改 deque 对象。

示例

// deque_crbegin.cpp
// compile with: /EHsc
#include <deque>
#include <iostream>

int main( )
{
   using namespace std;
   deque <int> v1;
   deque <int>::iterator v1_Iter;
   deque <int>::const_reverse_iterator v1_rIter;

   v1.push_back( 1 );
   v1.push_back( 2 );

   v1_Iter = v1.begin( );
   cout << "The first element of deque is "
        << *v1_Iter << "." << endl;

   v1_rIter = v1.crbegin( );
   cout << "The first element of the reversed deque is "
        << *v1_rIter << "." << endl;
}
The first element of deque is 1.
The first element of the reversed deque is 2.

crend

返回一个 const 迭代器,用于寻址反向 deque 中最后一个元素之后的位置。

const_reverse_iterator crend() const;

返回值

用于寻址反向 deque 中最后一个元素之后的位置(非反向 deque 中第一个元素之前的位置)的 const 反向随机访问迭代器。

注解

crend 用于反向 deque,正如 array::cend 用于 deque 一样。

由于使用 crend 的返回值(适当递减),因此不能修改 deque 对象。

crend 可用于测试反向迭代器是否已到达其 deque 的末尾。

不应对 crend 返回的值取消引用。

示例

// deque_crend.cpp
// compile with: /EHsc
#include <deque>
#include <iostream>

int main( )
{
   using namespace std;
   deque <int> v1;
   deque <int>::const_reverse_iterator v1_rIter;

   v1.push_back( 1 );
   v1.push_back( 2 );

   for ( v1_rIter = v1.rbegin( ) ; v1_rIter != v1.rend( ) ; v1_rIter++ )
      cout << *v1_rIter << endl;
}
2
1

deque

构造一个 deque,它具有特定大小或它的元素具有特定值,或具有特定分配器,或作为其他 deque 的全部或部分副本。

deque();

explicit deque(const Allocator& Al);
explicit deque(size_type Count);
deque(size_type Count, const Type& Val);

deque(
    size_type Count,
    const Type& Val,
    const Allocator& Al);

deque(const deque& Right);

template <class InputIterator>
deque(InputIterator First,  InputIterator Last);

template <class InputIterator>
deque(
   InputIterator First,
   InputIterator Last,
   const Allocator& Al);

deque(initializer_list<value_type> IList, const Allocator& Al);

参数

Al
要用于此对象的分配器类。

Count
构造的 deque 中的元素数。

Val
构造的 deque 中的元素值。

Right
所构造的 deque 要作为其副本的 deque

First
要复制的元素范围内的第一个元素的位置。

Last
要复制的元素范围外的第一个元素的位置。

IList
要复制的 initializer_list

备注

所有构造函数都存储一个分配器对象 (Al) 并初始化 deque

第一个构造函数指定一个空的初始 deque,第二个同样指定要使用的分配器类型 (_Al)。

第三个构造函数指定类 count 的默认值的指定数量 (Type) 的元素的重复。

第四个和第五个构造函数指定值为 val 的 (Count) 个元素的重复元素。

第六个构造函数指定 dequeRight 的副本。

第七个和第八个构造函数复制 deque 的范围 [First, Last)

第七个构造函数移动 dequeRight

第八个构造函数复制 initializer_list 的内容。

所有构造函数均不执行任何临时重新分配。

示例

/ compile with: /EHsc
#include <deque>
#include <iostream>
#include <forward_list>

int main()
{
    using namespace std;

    forward_list<int> f1{ 1, 2, 3, 4 };

    f1.insert_after(f1.begin(), { 5, 6, 7, 8 });

    deque <int>::iterator c1_Iter, c2_Iter, c3_Iter, c4_Iter, c5_Iter, c6_Iter;

    // Create an empty deque c0
    deque <int> c0;

    // Create a deque c1 with 3 elements of default value 0
    deque <int> c1(3);

    // Create a deque c2 with 5 elements of value 2
    deque <int> c2(5, 2);

    // Create a deque c3 with 3 elements of value 1 and with the
    // allocator of deque c2
    deque <int> c3(3, 1, c2.get_allocator());

    // Create a copy, deque c4, of deque c2
    deque <int> c4(c2);

    // Create a deque c5 by copying the range c4[ first,  last)
    c4_Iter = c4.begin();
    c4_Iter++;
    c4_Iter++;
    deque <int> c5(c4.begin(), c4_Iter);

    // Create a deque c6 by copying the range c4[ first,  last) and
    // c2 with the allocator of deque
    c4_Iter = c4.begin();
    c4_Iter++;
    c4_Iter++;
    c4_Iter++;
    deque <int> c6(c4.begin(), c4_Iter, c2.get_allocator());

    // Create a deque c8 by copying the contents of an initializer_list
    // using brace initialization
    deque<int> c8({ 1, 2, 3, 4 });

    initializer_list<int> iList{ 5, 6, 7, 8 };
    deque<int> c9( iList);

    cout << "c1 = ";
    for (int i : c1)
        cout << i << " ";
    cout << endl;

    cout << "c2 = ";
    for (int i : c2)
        cout << i << " ";
    cout << endl;

    cout << "c3 = ";
    for (int i : c3)
        cout << i << " ";
    cout << endl;

    cout << "c4 = ";
    for (int i : c4)
        cout << i << " ";
    cout << endl;

    cout << "c5 = ";
    for (int i : c5)
        cout << i << " ";
    cout << endl;

    cout << "c6 = ";
    for (int i : c6)
        cout << i << " ";
    cout << endl;

    // Move deque c6 to deque c7
    deque <int> c7(move(c6));
    deque <int>::iterator c7_Iter;

    cout << "c7 =";
    for (int i : c7)
        cout << i << " ";
    cout << endl;

    cout << "c8 = ";
    for (int i : c8)
        cout << i << " ";
    cout << endl;

    cout << "c9 = ";
    for (int i : c9)
        cout << i << " ";
    cout << endl;

    int x = 3;
}
// deque_deque.cpp
// compile with: /EHsc
#include <deque>
#include <iostream>

int main( )
{
    using namespace std;
   deque <int>::iterator c1_Iter, c2_Iter, c3_Iter, c4_Iter, c5_Iter, c6_Iter;

    // Create an empty deque c0
    deque <int> c0;

    // Create a deque c1 with 3 elements of default value 0
    deque <int> c1( 3 );

    // Create a deque c2 with 5 elements of value 2
    deque <int> c2( 5, 2 );

    // Create a deque c3 with 3 elements of value 1 and with the
    // allocator of deque c2
    deque <int> c3( 3, 1, c2.get_allocator( ) );

    // Create a copy, deque c4, of deque c2
    deque <int> c4( c2 );

    // Create a deque c5 by copying the range c4[ first,  last)
    c4_Iter = c4.begin( );
    c4_Iter++;
    c4_Iter++;
    deque <int> c5( c4.begin( ), c4_Iter );

    // Create a deque c6 by copying the range c4[ first,  last) and
    // c2 with the allocator of deque
    c4_Iter = c4.begin( );
   c4_Iter++;
   c4_Iter++;
   c4_Iter++;
   deque <int> c6( c4.begin( ), c4_Iter, c2.get_allocator( ) );

    // Create a deque c8 by copying the contents of an initializer_list
    // using brace initialization
    deque<int> c8({ 1, 2, 3, 4 });

        initializer_list<int> iList{ 5, 6, 7, 8 };
    deque<int> c9( iList);

    cout << "c1 = ";
    for ( c1_Iter = c1.begin( ); c1_Iter != c1.end( ); c1_Iter++ )
        cout << *c1_Iter << " ";
    cout << endl;

    cout << "c2 = ";
    for ( c2_Iter = c2.begin( ); c2_Iter != c2.end( ); c2_Iter++ )
        cout << *c2_Iter << " ";
    cout << endl;

    cout << "c3 = ";
    for ( c3_Iter = c3.begin( ); c3_Iter != c3.end( ); c3_Iter++ )
        cout << *c3_Iter << " ";
    cout << endl;

    cout << "c4 = ";
    for ( c4_Iter = c4.begin( ); c4_Iter != c4.end( ); c4_Iter++ )
        cout << *c4_Iter << " ";
    cout << endl;

    cout << "c5 = ";
    for ( c5_Iter = c5.begin( ); c5_Iter != c5.end( ); c5_Iter++ )
        cout << *c5_Iter << " ";
    cout << endl;

    cout << "c6 = ";
    for ( c6_Iter = c6.begin( ); c6_Iter != c6.end( ); c6_Iter++ )
        cout << *c6_Iter << " ";
    cout << endl;

    // Move deque c6 to deque c7
    deque <int> c7( move(c6) );
    deque <int>::iterator c7_Iter;

    cout << "c7 =" ;
    for ( c7_Iter = c7.begin( ) ; c7_Iter != c7.end( ) ; c7_Iter++ )
        cout << " " << *c7_Iter;
    cout << endl;

    cout << "c8 = ";
    for (int i : c8)
        cout << i << " ";
    cout << endl;

    cout << "c9 = ";
    or (int i : c9)
        cout << i << " ";
    cout << endl;
}

difference_type

一种类型,此类型提供引用同一 deque 中的元素的两个迭代器之间的差异。

typedef typename Allocator::difference_type difference_type;

备注

difference_type 也可以被描述为两个指针之间的元素数目。

示例

// deque_diff_type.cpp
// compile with: /EHsc
#include <iostream>
#include <deque>
#include <algorithm>

int main( )
{
   using namespace std;

   deque <int> c1;
   deque <int>::iterator c1_Iter, c2_Iter;

   c1.push_back( 30 );
   c1.push_back( 20 );
   c1.push_back( 30 );
   c1.push_back( 10 );
   c1.push_back( 30 );
   c1.push_back( 20 );

   c1_Iter = c1.begin( );
   c2_Iter = c1.end( );

   deque <int>::difference_type df_typ1, df_typ2, df_typ3;

   df_typ1 = count( c1_Iter, c2_Iter, 10 );
   df_typ2 = count( c1_Iter, c2_Iter, 20 );
   df_typ3 = count( c1_Iter, c2_Iter, 30 );
   cout << "The number '10' is in c1 collection " << df_typ1 << " times.\n";
   cout << "The number '20' is in c1 collection " << df_typ2 << " times.\n";
   cout << "The number '30' is in c1 collection " << df_typ3 << " times.\n";
}
The number '10' is in c1 collection 1 times.
The number '20' is in c1 collection 2 times.
The number '30' is in c1 collection 3 times.

emplace

将就地构造的元素插入到指定位置的 deque 中。

iterator emplace(
    const_iterator _Where,
    Type&& val);

参数

_Where
deque 中插入第一个元素的位置。

val
插入到 deque 中的元素的值。

返回值

该函数将返回一个指向 deque 中新元素的插入位置的迭代器。

注解

任何插入操作都可能产生巨额开销,请参阅 deque,了解有关 deque 性能的讨论。

示例

// deque_emplace.cpp
// compile with: /EHsc
#include <deque>
#include <iostream>

int main( )
{
   using namespace std;
   deque <int> v1;
   deque <int>::iterator Iter;

   v1.push_back( 10 );
   v1.push_back( 20 );
   v1.push_back( 30 );

   cout << "v1 =" ;
   for ( Iter = v1.begin( ) ; Iter != v1.end( ) ; Iter++ )
      cout << " " << *Iter;
   cout << endl;

// initialize a deque of deques by moving v1
   deque < deque <int> > vv1;

   vv1.emplace( vv1.begin(), move( v1 ) );
   if ( vv1.size( ) != 0 && vv1[0].size( ) != 0 )
      {
      cout << "vv1[0] =";
      for (Iter = vv1[0].begin( ); Iter != vv1[0].end( ); Iter++ )
         cout << " " << *Iter;
      cout << endl;
      }
}
v1 = 10 20 30
vv1[0] = 10 20 30

emplace_back

将就地构造的元素添加到 deque 的末尾。

void emplace_back(Type&& val);

参数

val
添加到 deque 末尾的元素。

示例

// deque_emplace_back.cpp
// compile with: /EHsc
#include <deque>
#include <iostream>

int main( )
{
   using namespace std;
   deque <int> v1;

   v1.push_back( 1 );
   if ( v1.size( ) != 0 )
      cout << "Last element: " << v1.back( ) << endl;

   v1.push_back( 2 );
   if ( v1.size( ) != 0 )
      cout << "New last element: " << v1.back( ) << endl;

// initialize a deque of deques by moving v1
   deque < deque <int> > vv1;

   vv1.emplace_back( move( v1 ) );
   if ( vv1.size( ) != 0 && vv1[0].size( ) != 0 )
      cout << "Moved last element: " << vv1[0].back( ) << endl;
}
Last element: 1
New last element: 2
Moved last element: 2

emplace_front

将就地构造的元素添加到 deque 的末尾。

void emplace_front(Type&& val);

参数

val
要添加到 deque 开头的元素。

示例

// deque_emplace_front.cpp
// compile with: /EHsc
#include <deque>
#include <iostream>

int main( )
{
   using namespace std;
   deque <int> v1;

   v1.push_back( 1 );
   if ( v1.size( ) != 0 )
      cout << "Last element: " << v1.back( ) << endl;

   v1.push_back( 2 );
   if ( v1.size( ) != 0 )
      cout << "New last element: " << v1.back( ) << endl;

// initialize a deque of deques by moving v1
   deque < deque <int> > vv1;

   vv1.emplace_front( move( v1 ) );
   if ( vv1.size( ) != 0 && vv1[0].size( ) != 0 )
      cout << "Moved last element: " << vv1[0].back( ) << endl;
}
Last element: 1
New last element: 2
Moved last element: 2

empty

测试 deque 是否为空。

bool empty() const;

返回值

如果 deque 为空,则为 true;如果 deque 不为空,则为 false

示例

// deque_empty.cpp
// compile with: /EHsc
#include <deque>
#include <iostream>

int main( )
{
   using namespace std;
   deque <int> c1;

   c1.push_back( 10 );
   if ( c1.empty( ) )
      cout << "The deque is empty." << endl;
   else
      cout << "The deque is not empty." << endl;
}
The deque is not empty.

end

返回一个迭代器,此迭代器用于发现 deque 中最后一个元素之后的位置。

const_iterator end() const;

iterator end();

返回值

用于发现 deque 中最后一个元素之后的位置的随机访问迭代器。 如果 deque 为空,则 deque::end == deque::begin

备注

end 用于测试迭代器是否已到达其 deque 的末尾。

示例

// deque_end.cpp
// compile with: /EHsc
#include <deque>
#include <iostream>

int main( )
{
   using namespace std;
   deque <int> c1;
   deque <int>::iterator c1_Iter;

   c1.push_back( 10 );
   c1.push_back( 20 );
   c1.push_back( 30 );

   c1_Iter = c1.end( );
   c1_Iter--;
   cout << "The last integer of c1 is " << *c1_Iter << endl;

   c1_Iter--;
   *c1_Iter = 400;
   cout << "The new next-to-last integer of c1 is " << *c1_Iter << endl;

   // If a const iterator had been declared instead with the line:
   // deque <int>::const_iterator c1_Iter;
   // an error would have resulted when inserting the 400

   cout << "The deque is now:";
   for ( c1_Iter = c1.begin( ); c1_Iter != c1.end( ); c1_Iter++ )
      cout << " " << *c1_Iter;
}
The last integer of c1 is 30
The new next-to-last integer of c1 is 400
The deque is now: 10 400 30

erase

从指定位置删除 deque 中一个或一系列元素。

iterator erase(iterator _Where);

iterator erase(iterator first, iterator last);

参数

_Where
要从 deque 中移除的元素的位置。

first
要从 deque 中移除的第一个元素的位置。

last
要从 deque 中移除的刚超出最后一个元素的位置。

返回值

指定已移除的任何元素之外保留的第一个元素的随机访问迭代器;如果不存在此类元素,则为指向 deque 末尾的指针。

注解

erase 绝不会引发异常。

示例

// deque_erase.cpp
// compile with: /EHsc
#include <deque>
#include <iostream>

int main( )
{
   using namespace std;
   deque <int> c1;
   deque <int>::iterator Iter;

   c1.push_back( 10 );
   c1.push_back( 20 );
   c1.push_back( 30 );
   c1.push_back( 40 );
   c1.push_back( 50 );
   cout << "The initial deque is: ";
   for ( Iter = c1.begin( ); Iter != c1.end( ); Iter++ )
      cout << *Iter << " ";
   cout << endl;
   c1.erase( c1.begin( ) );
   cout << "After erasing the first element, the deque becomes:  ";
   for ( Iter = c1.begin( ); Iter != c1.end( ); Iter++ )
      cout << *Iter << " ";
   cout << endl;
   Iter = c1.begin( );
   Iter++;
   c1.erase( Iter, c1.end( ) );
   cout << "After erasing all elements but the first, deque becomes: ";
   for ( Iter = c1.begin( ); Iter != c1.end( ); Iter++ )
      cout << *Iter << " ";
   cout << endl;
}
The initial deque is: 10 20 30 40 50
After erasing the first element, the deque becomes:  20 30 40 50
After erasing all elements but the first, deque becomes: 20

front

返回对 deque 中第一个元素的引用。

reference front();

const_reference front() const;

返回值

如果 deque 为空,则返回的结果不确定。

备注

如果将 front 的返回值分配给 const_reference,则可以修改 deque 对象。 如果将 front 的返回值分配给 reference,则可以修改 deque 对象。

当使用定义为 1 或 2 的 _ITERATOR_DEBUG_LEVEL 进行编译时,如果试图访问空 deque 中的元素,则将发生运行时错误。 有关更多信息,请参阅经过检查的迭代器

示例

// deque_front.cpp
// compile with: /EHsc
#include <deque>
#include <iostream>

int main( )
{
   using namespace std;
   deque <int> c1;

   c1.push_back( 10 );
   c1.push_back( 11 );

   int& i = c1.front( );
   const int& ii = c1.front( );

   cout << "The first integer of c1 is " << i << endl;
   i++;
   cout << "The second integer of c1 is " << ii << endl;
}
The first integer of c1 is 10
The second integer of c1 is 11

get_allocator

返回用于构造 deque 的分配器对象的一个副本。

Allocator get_allocator() const;

返回值

deque 使用的分配器。

注解

deque 类的分配器指定类管理存储的方式。 C++ 标准库容器类提供的默认分配器足以满足大多编程需求。 编写和使用你自己的分配器类是高级 C++ 主题。

示例

// deque_get_allocator.cpp
// compile with: /EHsc
#include <deque>
#include <iostream>

int main( )
{
   using namespace std;
   // The following lines declare objects that use the default allocator.
   deque <int> c1;
   deque <int, allocator<int> > c2 = deque <int, allocator<int> >( allocator<int>( ) );

   // c3 will use the same allocator class as c1
   deque <int> c3( c1.get_allocator( ) );

   deque <int>::allocator_type xlst = c1.get_allocator( );
   // You can now call functions on the allocator class used by c1
}

insert

将一个、多个或一系列元素插入 deque 中的指定位置。

iterator insert(
    const_iterator Where,
    const Type& Val);

iterator insert(
    const_iterator Where,
    Type&& Val);

void insert(
    iterator Where,
    size_type Count,
    const Type& Val);

template <class InputIterator>
void insert(
    iterator Where,
    InputIterator First,
    InputIterator Last);

iterator insert(
    iterator Where,initializer_list<Type>
IList);

参数

Where
目标 deque 中插入第一个元素的位置。

Val
插入到 deque 中的元素的值。

Count
要插入 deque 中的元素数目。

First
要从参数 deque 中复制的一系列元素中第一个元素的位置。

Last
要从参数 deque 中复制的一系列元素以外的第一个元素的位置。

IList
要插入的元素的 initializer_list

返回值

前两个插入函数返回一个迭代器,该迭代器指向新元素插入到 deque 中的位置。

备注

任何插入操作都可能产生巨额开销。

iterator

一种类型,此类型提供可读取或修改 deque 中的任何元素的随机访问迭代器。

typedef implementation-defined iterator;

备注

iterator 类型可用于修改元素的值。

示例

请参阅 begin 的示例。

max_size

返回 deque 的最大长度。

size_type max_size() const;

返回值

deque 可能的最大长度。

示例

// deque_max_size.cpp
// compile with: /EHsc
#include <deque>
#include <iostream>

int main( )
{
   using namespace std;
   deque <int> c1;
   deque <int>::size_type i;

   i = c1.max_size( );
   cout << "The maximum possible length of the deque is " << i << "." << endl;
}

operator[]

返回对指定位置的 deque 元素的引用。

reference operator[](size_type pos);

const_reference operator[](size_type pos) const;

参数

pos
要引用的 deque 元素的位置。

返回值

对参数中指定其位置的元素的引用。 如果指定的位置大于 deque 的大小,则结果为不确定。

备注

如果将 operator[] 的返回值分配给 const_reference,则可以修改 deque 对象。 如果将 operator[] 的返回值分配给 reference,则可以修改 deque 对象。

当使用定义为 1 或 2 的 _ITERATOR_DEBUG_LEVEL 进行编译时,如果试图访问 deque 边界之外的元素,将发生运行时错误。 有关更多信息,请参阅经过检查的迭代器

示例

// deque_op_ref.cpp
// compile with: /EHsc
#include <deque>
#include <iostream>

int main( )
{
   using namespace std;
   deque <int> c1;

   c1.push_back( 10 );
   c1.push_back( 20 );
   cout << "The first integer of c1 is " << c1[0] << endl;
   int& i = c1[1];
   cout << "The second integer of c1 is " << i << endl;
}
The first integer of c1 is 10
The second integer of c1 is 20

operator=

使用另一个 deque 的元素替换该 deque 的元素。

deque& operator=(const deque& right);

deque& operator=(deque&& right);

参数

right
提供新内容的 deque

注解

第一次重写会将元素复制到分配源 right 中的 deque。 第二次重写将元素移动到 right 中的 deque

执行该运算符之前移除此 deque 中包含的元素。

示例

// deque_operator_as.cpp
// compile with: /EHsc
#include <deque>
#include <iostream>
using namespace std;

typedef deque<int> MyDeque;

template<typename MyDeque> struct S;

template<typename MyDeque> struct S<MyDeque&> {
  static void show( MyDeque& d ) {
    MyDeque::const_iterator iter;
    for (iter = d.cbegin(); iter != d.cend(); iter++)
       cout << *iter << " ";
    cout << endl;
  }
};

template<typename MyDeque> struct S<MyDeque&&> {
  static void show( MyDeque&& d ) {
    MyDeque::const_iterator iter;
    for (iter = d.cbegin(); iter != d.cend(); iter++)
       cout << *iter << " ";
cout << " via unnamed rvalue reference " << endl;
  }
};

int main( )
{
   MyDeque d1, d2;

   d1.push_back(10);
   d1.push_back(20);
   d1.push_back(30);
   d1.push_back(40);
   d1.push_back(50);

   cout << "d1 = " ;
   S<MyDeque&>::show( d1 );

   d2 = d1;
   cout << "d2 = ";
   S<MyDeque&>::show( d2 );

   cout << "     ";
   S<MyDeque&&>::show ( move< MyDeque& > (d1) );
}

pointer

提供指向 deque 中元素的指针。

typedef typename Allocator::pointer pointer;

注解

pointer 类型可用于修改元素的值。 iterator 更常用于访问 deque 元素。

pop_back

删除 deque 末尾处的元素。

void pop_back();

备注

最后一个元素不得为空。 pop_back 绝不会引发异常。

示例

// deque_pop_back.cpp
// compile with: /EHsc
#include <deque>
#include <iostream>

int main( )
{
   using namespace std;
   deque <int> c1;

   c1.push_back( 1 );
   c1.push_back( 2 );
   cout << "The first element is: " << c1.front( ) << endl;
   cout << "The last element is: " << c1.back( ) << endl;

   c1.pop_back( );
   cout << "After deleting the element at the end of the deque, the "
      "last element is: " << c1.back( ) << endl;
}
The first element is: 1
The last element is: 2
After deleting the element at the end of the deque, the last element is: 1

pop_front

删除 deque 开头的元素。

void pop_front();

备注

第一个元素不得为空。 pop_front 绝不会引发异常。

示例

// deque_pop_front.cpp
// compile with: /EHsc
#include <deque>
#include <iostream>

int main( )
{
   using namespace std;
   deque <int> c1;

   c1.push_back( 1 );
   c1.push_back( 2 );
   cout << "The first element is: " << c1.front( ) << endl;
   cout << "The second element is: " << c1.back( ) << endl;

   c1.pop_front( );
   cout << "After deleting the element at the beginning of the "
      "deque, the first element is: " << c1.front( ) << endl;
}
The first element is: 1
The second element is: 2
After deleting the element at the beginning of the `deque`, the first element is: 2

push_back

将元素添加到 deque 的末尾。

void push_back(const Type& val);

void push_back(Type&& val);

参数

val
添加到 deque 末尾的元素。

备注

如果引发了异常,deque 将保持不变,该异常将被重新引发。

push_front

deque 的开头添加元素。

void push_front(const Type& val);
void push_front(Type&& val);

参数

val
要添加到 deque 开头的元素。

备注

如果引发了异常,deque 将保持不变,该异常将被重新引发。

示例

// deque_push_front.cpp
// compile with: /EHsc
#include <deque>
#include <iostream>
#include <string>

int main( )
{
   using namespace std;
   deque <int> c1;

   c1.push_front( 1 );
   if ( c1.size( ) != 0 )
      cout << "First element: " << c1.front( ) << endl;

   c1.push_front( 2 );
   if ( c1.size( ) != 0 )
      cout << "New first element: " << c1.front( ) << endl;

// move initialize a deque of strings
   deque <string> c2;
   string str("a");

   c2.push_front( move( str ) );
   cout << "Moved first element: " << c2.front( ) << endl;
}
First element: 1
New first element: 2
Moved first element: a

rbegin

返回指向反向 deque 中第一个元素的迭代器。

const_reverse_iterator rbegin() const;

reverse_iterator rbegin();

返回值

发现反向 deque 中的第一个元素或发现曾是非反向 deque 中的最后一个元素的反向随机访问迭代器。

备注

rbegin 用于反向 deque,正如 begin 用于 deque 一样。

如果将 rbegin 的返回值分配给 const_reverse_iterator,则可以修改 deque 对象。 如果将 rbegin 的返回值分配给 reverse_iterator,则可以修改 deque 对象。

rbegin 可用于向后循环访问 deque

示例

// deque_rbegin.cpp
// compile with: /EHsc
#include <deque>
#include <iostream>

int main( )
{
   using namespace std;
   deque <int> c1;
   deque <int>::iterator c1_Iter;
   deque <int>::reverse_iterator c1_rIter;

   // If the following line had replaced the line above, an error
   // would have resulted in the line modifying an element
   // (commented below) because the iterator would have been const
   // deque <int>::const_reverse_iterator c1_rIter;

   c1.push_back( 10 );
   c1.push_back( 20 );
   c1.push_back( 30 );

   c1_rIter = c1.rbegin( );
   cout << "Last element in the deque is " << *c1_rIter << "." << endl;

   cout << "The deque contains the elements: ";
   for ( c1_Iter = c1.begin( ); c1_Iter != c1.end( ); c1_Iter++ )
      cout << *c1_Iter << " ";
   cout << "in that order.";
   cout << endl;

   // rbegin can be used to iterate through a deque in reverse order
   cout << "The reversed deque is: ";
   for ( c1_rIter = c1.rbegin( ); c1_rIter != c1.rend( ); c1_rIter++ )
      cout << *c1_rIter << " ";
   cout << endl;

   c1_rIter = c1.rbegin( );
   *c1_rIter = 40;  // This would have caused an error if a
                    // const_reverse iterator had been declared as
                    // noted above
   cout << "Last element in deque is now " << *c1_rIter << "." << endl;
}
Last element in the deque is 30.
The deque contains the elements: 10 20 30 in that order.
The reversed deque is: 30 20 10
Last element in deque is now 40.

reference

一种类型,此类型提供对存储在 deque 中的元素的引用。

typedef typename Allocator::reference reference;

示例

// deque_reference.cpp
// compile with: /EHsc
#include <deque>
#include <iostream>

int main( )
{
   using namespace std;
   deque <int> c1;

   c1.push_back( 10 );
   c1.push_back( 20 );

   const int &i = c1.front( );
   int &j = c1.back( );
   cout << "The first element is " << i << endl;
   cout << "The second element is " << j << endl;
}
The first element is 10
The second element is 20

rend

返回一个迭代器,此迭代器用于发现反向 deque 中最后一个元素之后的位置。

const_reverse_iterator rend() const;

reverse_iterator rend();

返回值

用于寻址反向 deque 中最后一个元素之后的位置(非反向 deque 中第一个元素之前的位置)的反向随机访问迭代器。

注解

rend 用于反向 deque,正如 end 用于 deque 一样。

如果将 rend 的返回值分配给 const_reverse_iterator,则可以修改 deque 对象。 如果将 rend 的返回值分配给 reverse_iterator,则可以修改 deque 对象。

rend 可用于测试反向迭代器是否已到达其 deque 末尾。

不应对 rend 返回的值取消引用。

示例

// deque_rend.cpp
// compile with: /EHsc
#include <deque>
#include <iostream>

int main( )
{
   using namespace std;

   deque <int> c1;
   deque <int>::iterator c1_Iter;
   deque <int>::reverse_iterator c1_rIter;
   // If the following line had replaced the line above, an error
   // would have resulted in the line modifying an element
   // (commented below) because the iterator would have been const
   // deque <int>::const_reverse_iterator c1_rIter;

   c1.push_back( 10 );
   c1.push_back( 20 );
   c1.push_back( 30 );

   c1_rIter = c1.rend( );
   c1_rIter --; // Decrementing a reverse iterator moves it forward
                // in the deque (to point to the first element here)
   cout << "The first element in the deque is: " << *c1_rIter << endl;

   cout << "The deque is: ";
   for ( c1_Iter = c1.begin( ); c1_Iter != c1.end( ); c1_Iter++ )
      cout << *c1_Iter << " ";
   cout << endl;

   // rend can be used to test if an iteration is through all of
   // the elements of a reversed deque
   cout << "The reversed deque is: ";
   for ( c1_rIter = c1.rbegin( ); c1_rIter != c1.rend( ); c1_rIter++ )
      cout << *c1_rIter << " ";
   cout << endl;

   c1_rIter = c1.rend( );
   c1_rIter--; // Decrementing the reverse iterator moves it backward
               // in the reversed deque (to the last element here)
   *c1_rIter = 40; // This modification of the last element would
                   // have caused an error if a const_reverse
                   // iterator had been declared (as noted above)
   cout << "The modified reversed deque is: ";
   for ( c1_rIter = c1.rbegin( ); c1_rIter != c1.rend( ); c1_rIter++ )
      cout << *c1_rIter << " ";
   cout << endl;
}
The first element in the deque is: 10
The deque is: 10 20 30
The reversed deque is: 30 20 10
The modified reversed deque is: 30 20 40

resize

deque 指定新的大小。

void resize(size_type _Newsize);

void resize(size_type _Newsize, Type val);

参数

_Newsize
deque 的新大小。

val
新的大小大于原始大小时要添加至 deque 的新元素的值。 如果省略此值,则会赋给新元素此类的默认值。

备注

如果 dequesize 小于 _Newsize,则会在其达到大小 _Newsize 时将元素添加到 deque

如果 dequesize 大于 _Newsize,那么最接近 deque 末尾的元素将被删除,直到 deque 达到大小 _Newsize

如果 deque 的当前大小与 _Newsize 相同,则不采取任何操作。

size 表示 deque 的当前大小。

示例

// deque_resize.cpp
// compile with: /EHsc
#include <deque>
#include <iostream>

int main( )
{
   using namespace std;
   deque <int> c1;

   c1.push_back( 10 );
   c1.push_back( 20 );
   c1.push_back( 30 );

   c1.resize( 4,40 );
   cout << "The size of c1 is: " << c1.size( ) << endl;
   cout << "The value of the last element is " << c1.back( ) << endl;

   c1.resize( 5 );
   cout << "The size of c1 is now: " << c1.size( ) << endl;
   cout << "The value of the last element is now " << c1.back( ) << endl;

   c1.resize( 2 );
   cout << "The reduced size of c1 is: " << c1.size( ) << endl;
   cout << "The value of the last element is now " << c1.back( ) << endl;
}
The size of c1 is: 4
The value of the last element is 40
The size of c1 is now: 5
The value of the last element is now 0
The reduced size of c1 is: 2
The value of the last element is now 20

reverse_iterator

提供可读取或修改反向 deque 中元素的随机访问迭代器的类型。

typedef std::reverse_iterator<iterator> reverse_iterator;

备注

reverse_iterator 类型用于循环访问 deque

示例

请参阅 rbegin 的示例。

shrink_to_fit

放弃额外容量。

void shrink_to_fit();

备注

没有可移植方法来确定 shrink_to_fit 是否可以减少 deque 使用的存储。

示例

// deque_shrink_to_fit.cpp
// compile with: /EHsc
#include <deque>
#include <iostream>

int main( )
{
   using namespace std;
   deque <int> v1;
   //deque <int>::iterator Iter;

   v1.push_back( 1 );
   v1.push_back( 2 );
   cout << "Current size of v1 = "
      << v1.size( ) << endl;
   v1.shrink_to_fit();
   cout << "Current size of v1 = "
      << v1.size( ) << endl;
}
Current size of v1 = 1
Current size of v1 = 1

size

返回 deque 中的元素数量。

size_type size() const;

返回值

deque 的当前长度。

示例

// deque_size.cpp
// compile with: /EHsc
#include <deque>
#include <iostream>

int main( )
{
   using namespace std;
   deque <int> c1;
   deque <int>::size_type i;

   c1.push_back( 1 );
   i = c1.size( );
   cout << "The deque length is " << i << "." << endl;

   c1.push_back( 2 );
   i = c1.size( );
   cout << "The deque length is now " << i << "." << endl;
}
The deque length is 1.
The deque length is now 2.

size_type

一种类型,此类型计算 deque 中的元素数目。

typedef typename Allocator::size_type size_type;

示例

请参阅 size 的示例。

swap

交换两个 deque 的元素。

void swap(deque<Type, Allocator>& right);

friend void swap(deque<Type, Allocator>& left, deque<Type, Allocator>& right) template <class Type, class Allocator>
void swap(deque<Type, Allocator>& left, deque<Type, Allocator>& right);

参数

right
deque 提供要交换的元素,或者其元素将与 dequeleft 的元素交换的 deque

left
其元素将与 dequeright 的进行交换的 deque

示例

// deque_swap.cpp
// compile with: /EHsc
#include <deque>
#include <iostream>

int main( )
{
   using namespace std;
   deque <int> c1, c2, c3;
   deque <int>::iterator c1_Iter;

   c1.push_back( 1 );
   c1.push_back( 2 );
   c1.push_back( 3 );
   c2.push_back( 10 );
   c2.push_back( 20 );
   c3.push_back( 100 );

   cout << "The original deque c1 is:";
   for ( c1_Iter = c1.begin( ); c1_Iter != c1.end( ); c1_Iter++ )
      cout << " " << *c1_Iter;
   cout << endl;

   c1.swap( c2 );

   cout << "After swapping with c2, deque c1 is:";
   for ( c1_Iter = c1.begin( ); c1_Iter != c1.end( ); c1_Iter++ )
      cout << " " << *c1_Iter;
   cout << endl;

   swap( c1,c3 );

   cout << "After swapping with c3, deque c1 is:";
   for ( c1_Iter = c1.begin( ); c1_Iter != c1.end( ); c1_Iter++ )
      cout << " " << *c1_Iter;
   cout << endl;

   swap<>(c1, c2);
   cout << "After swapping with c2, deque c1 is:";
   for ( c1_Iter = c1.begin( ); c1_Iter != c1.end( ); c1_Iter++ )
      cout << " " << *c1_Iter;
   cout << endl;
}
The original deque c1 is: 1 2 3
After swapping with c2, deque c1 is: 10 20
After swapping with c3, deque c1 is: 100
After swapping with c2, deque c1 is: 1 2 3

value_type

一种类型,此类型表示存储在 deque 中的数据类型。

typedef typename Allocator::value_type value_type;

注解

value_type 是模板参数 Type 的同义词。

示例

// deque_value_type.cpp
// compile with: /EHsc
#include <deque>
#include <iostream>
int main( )
{
   using namespace std;
   deque<int>::value_type AnInt;
   AnInt = 44;
   cout << AnInt << endl;
}
44

另请参阅

C++ 标准库中的线程安全
C++ 标准库参考