Share via


deque 類別

以線性排列方式排列指定類型的項目,並且像向量一樣允許快速隨機存取任何項目,可有效率地在容器背面插入和刪除。 不過,與向量不同的是,deque 類別也支援在此容器前面有效率的插入和刪除。

語法

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

參數

Type
要存放在 deque 中的項目資料類型。

Allocator
型別,表示預存配置器物件,該物件會封裝記憶體配置和解除分配的詳細 deque數據。 這個引數是選用引數,且預設值是 allocator<Type>

備註

選擇容器類型時,通常應根據應用程式所需的搜尋和插入類型。 Vectors 當隨機存取任何元素處於進階階段時,應該為管理序列的慣用容器,而且只有在序列結尾才需要插入或刪除專案。 如果您非常需要在序列內的任何位置有效地插入和刪除項目 (定時),則清單容器的效能會更優異。 在此序列中間的這類作業需要複製和指派項目,且和此序列中的項目數目成比例 (線性時間)。

Deque 當成員函式必須插入或清除序列的元素時,就會重新配置:

  • 如果元素插入空序列中,或清除元素以離開空序列,則先前傳 begin 回並 end 變成無效的反覆運算器。

  • 如果專案插入至 的第一個位置 deque,則所有反覆運算器,但沒有任何參考,指定現有元素會變成無效。

  • 如果在 的結尾 deque插入元素,則 end 和所有反覆運算器,但沒有參考,指定現有元素會變成無效。

  • 如果專案在 前面 deque清除,則只有該反覆運算器和已清除元素的參考會變成無效。

  • 如果最後一個專案從的結尾 deque清除,則只有最後一個專案的反覆運算器,且清除之元素的參考會變成無效。

否則,插入或刪除一個項目會讓所有迭代器和參考失效。

成員

建構函式

名稱 描述
deque 建構 deque。 提供數個建構函式,以不同的方式設定新的 deque 內容:空的、以指定數目的空白元素載入、從另 deque一個移動或複製的內容、使用反覆運算器複製或行動的內容,以及複製到時間的 dequecount 一個元素。 有些建構函式可使用自訂的 allocator 來建立項目。

Typedefs

名稱 描述
allocator_type 類型,表示 allocator 物件的 deque 類別。
const_iterator 類型,提供可以存取和讀取 deque 中項目做為 const 的隨機存取迭代器。
const_pointer 型別,提供 中 deque 專案的指標做為 const
const_reference 型別,提供 中 deque 項目的參考,以便讀取和其他作業為 const
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 const 反覆運算器傳回 至中的 deque第一個專案。
cend 傳回指向 deque 結尾之外的 const 隨機存取迭代器。
clear 清除 deque 的所有項目。
crbegin 將隨機存取 const 反覆運算器傳回以反向順序檢視的第 deque 一個專案。
crend 將隨機存取 const 反覆運算器傳回以反向順序檢視的第 deque 一個專案。
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
initializer_list 插入 的 deque

備註

清除目標 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 大於 的大小 dequeat 則會擲回例外狀況。

備註

如果 的傳回值 at 指派給 const_referencedeque 則無法修改 物件。 如果 的傳回值 at 指派給 referencedeque 則可以修改 物件。

範例

// 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_referencedeque 則無法修改 物件。 如果 的傳回值 back 指派給 referencedeque 則可以修改 物件。

使用 _ITERATOR_DEBUG_LEVEL 定義為 1 或 2 進行編譯時,如果您嘗試存取空 deque中的專案,就會發生運行時錯誤。 如需詳細資訊,請參閱 Checked Iterators

範例

// 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_iteratordeque 則無法修改 物件。 如果將 的 begin 傳回值指派給 iteratordeque 則可以修改 物件。

範例

// 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

類型,提供隨機存取反覆運算器,可存取和讀取 const 中的 deque專案。

typedef implementation-defined const_iterator;

備註

類型 const_iterator 無法用來修改專案的值。

範例

請參閱 back 的範例。

const_pointer

const提供中項目指標deque

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

類型,提供隨機存取反覆運算器,可讀取 const 中的任何 deque專案。

typedef std::reverse_iterator<const_iterator> const_reverse_iterator;

備註

const_reverse_iterator 別無法修改專案的值,並用來反向逐一查看 deque

範例

如需如何宣告和使用反覆運算器的範例 rbegin ,請參閱範例。

crbegin

const 反覆運算器傳回反轉 deque的第一個專案。

const_reverse_iterator crbegin() const;

傳回值

const反向隨機存取反覆運算器,尋址反轉deque中的第一個專案,或尋址未反deque轉 中的最後一個專案。

備註

使用 的 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;

傳回值

const反向隨機存取反覆運算器,尋址對像是反轉中最後一個項目之後的位置(在未反dequedeque的第一個元素之前的位置)。

備註

crend與 反轉deque搭配使用,就像 搭配 使用一dequearray::cend

使用 的傳回值 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
要從中複製所建構之 dequedeque

First
項目範圍中要複製的第一個項目位置。

Last
項目範圍之外要複製的第一個項目位置。

IList
要複製的 initializer_list

備註

所有建構函式都會儲存設定器物件 (Al) 並初始化 deque

前兩個建構函式會指定空的初始 deque;第二個建構函式也會指定要使用的配置器類型 (_Al)。

第三個建構函式會指定類別 count 之指定數目 (Type) 的項目重複預設值。

第四個和第五個建構函式會指定 值 的重複專案Countval

第六個建構函式會指定的 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;

傳回值

truedeque如果 是空的,則為 ,false如果 不是空的deque,則為 。

範例

// 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_referencedeque 則無法修改 物件。 如果 的傳回值 front 指派給 referencedeque 則可以修改 物件。

使用 _ITERATOR_DEBUG_LEVEL 定義為 1 或 2 進行編譯時,如果您嘗試存取空 deque中的專案,就會發生運行時錯誤。 如需詳細資訊,請參閱 Checked Iterators

範例

// 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要插入之項目的 。

傳回值

前兩個 insert 函式會傳回反覆運算器,指向插入新專案的位置 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_referencedeque 則無法修改 物件。 如果 的傳回值 operator[] 指派給 referencedeque 則可以修改 物件。

使用 定義為 1 或 2 進行 _ITERATOR_DEBUG_LEVEL 編譯時,如果您嘗試存取 超出 界限的專案 deque,就會發生運行時錯誤。 如需詳細資訊,請參閱 Checked Iterators

範例

// 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搭配使用,就像 搭配 使用一dequebegin

如果 的傳回值 rbegin 指派給 const_reverse_iteratordeque 則無法修改 物件。 如果 的傳回值 rbegin 指派給 reverse_iteratordeque 則可以修改 物件。

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();

傳回值

反向隨機存取反覆運算器,尋址對像是反轉中最後一個項目之後的位置(在未反dequedeque的第一個元素之前的位置)。

備註

rend與 反轉deque搭配使用,就像 搭配 使用一dequeend

如果 的傳回值 rend 指派給 const_reverse_iteratordeque 則無法修改 物件。 如果 的傳回值 rend 指派給 reverse_iteratordeque 則可以修改 物件。

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 的新元素值。 如果省略此值,就會為新元素指派類別的預設值。

備註

deque如果 的 size 小於 _Newsize,元素會加入至 ,deque直到到達大小_Newsize為止。

deque如果的 size 大於 _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 要交換的專案,或 deque 要與 的 dequeleft元素交換的 。

left
deque,其專案要與的dequeright項目交換。

範例

// 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++ 標準程式庫參考