deque クラス

特定の型の要素をベクターのような線形の配置に整列し、任意の要素への高速なランダム アクセス、およびコンテナーの後ろでの効率的な挿入と削除を行えるようにします。 ただし、ベクターとは異なり、deque クラスは、コンテナーの前での効率的な挿入と削除もサポートします。

構文

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

パラメーター

Type
deque に格納される要素のデータ型。

Allocator
メモリの deque の割り当てと解放に関する詳細をカプセル化する、格納されたアロケーター オブジェクトを表す型。 この引数は省略可能であり、既定値は allocator<Type> です。

解説

一般的に、コンテナー型の選択は、アプリケーションにおいて必要な検索および挿入の種類に基づいている必要があります。 Vectors は、任意の要素へのランダム アクセスが Premium にあり、要素の挿入または削除がシーケンスの最後にのみ必要な場合に、シーケンスを管理するための優先コンテナーである必要があります。 リスト コンテナーでは、シーケンス内の任意の位置における (定数時間での) 効率的な挿入と削除が優先事項である場合に、より優れたパフォーマンスになります。 シーケンスの途中でこのような操作を行うには、シーケンス (線形時間) 内の要素数に比例した要素のコピーおよび割り当てが必要になります。

Deque の再割り当ては、メンバー関数がシーケンスの要素を挿入または消去する必要がある場合に発生します。

  • 空のシーケンスに要素が挿入された場合、または空のシーケンスを残すために要素が消去された場合は、以前に返された反復子がbeginend無効になります。

  • 要素が deque の最初の位置に挿入された場合は、既存の要素を指定するすべての反復子 (参照を除く) が無効になります。

  • 要素が deque の末尾に挿入された場合は、end および既存の要素を指定するすべての反復子 (参照を除く) が無効になります。

  • 要素が deque の前で消去された場合は、その反復子と消去された要素へ参照のみが無効になります。

  • 最後の要素が deque の末尾から消去された場合は、最後の要素への反復子と消去された要素への参照のみが無効になります。

それ以外の場合、要素を挿入または消去すると、すべての反復子および参照が無効になります。

メンバー

コンストラクター

名前 説明
deque deque を構築します。 異なる方法で新しい deque の内容を設定するための複数のコンストラクターが用意されています (空の場合に指定した空の要素数で読み込む、別の deque から内容をコピーまたは移動する、反復子を使用して内容をコピーまたは移動する、1 つの要素を dequecount 回コピーするなど)。 一部のコンストラクターでは、要素を作成するためのカスタムの allocator を使用できます。

Typedefs

名前 説明
allocator_type allocator オブジェクトの deque クラスを表す型。
const_iterator deque 内の要素にアクセスし、読み取ることができるランダム アクセス反復子を const として提供する型。
const_pointer as 内の要素dequeconstへのポインターを提供する型。
const_reference 読み取りおよびその他の操作const用に要素への参照をdeque提供する型。
const_reverse_iterator deque 内の要素にアクセスし、読み取ることができるランダム アクセス反復子を const として提供する型。 deque は逆に表示されます。 詳細については、reverse_iterator クラスを参照してください
difference_type 同じ deque 内の要素を参照する 2 つのランダム アクセス反復子の違いを提供する型。
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 const の末尾の次の位置を指し示すランダム アクセス deque 反復子を返します。
clear deque のすべての要素を消去します。
crbegin deque 内の最初の要素へのランダム アクセス const 反復子を返します。
crend deque 内の最初の要素へのランダム アクセス const 反復子を返します。
emplace 指定した位置において、構築された要素を deque の適切な場所に挿入します。
emplace_back 構築された要素を deque の末尾の適切な場所に追加します。
emplace_front 構築された要素を deque の先頭の適切な場所に追加します。
empty true に含まれている要素がゼロ個の場合に deque を返し、1 つ以上の要素が含まれている場合に false を返します。
end deque の末尾の次の位置を指し示すランダム アクセス反復子を返します。
erase 指定した位置から deque の要素または要素範囲を削除します。
front deque 内の最初の要素への参照を返します。
get_allocator allocator の構築に使用される deque オブジェクトのコピーを返します。
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 2 つの 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 内で参照する要素を示す添字 (または位置の番号)。

戻り値

posdeque のサイズより大きい場合、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 オブジェクトを変更できます。

_ITERATOR_DEBUG_LEVEL を 1 または 2 に定義してコンパイルすると、空の 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 型推論キーワードと共に使用します。 例では、Containerconstbegin() をサポートする任意の種類の変更可能な (非 cbegin()) コンテナーであると見なします。

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 型推論キーワードと共に使用します。 例では、Containerconstend() をサポートする任意の種類の変更可能な (非 cend()) コンテナーであると見なします。

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

deque 内の const 要素にアクセスし、読み取ることができるランダム アクセス反復子を提供する型。

typedef implementation-defined const_iterator;

解説

const_iterator 型で要素の値を変更することはできません。

back の例を参照してください。

const_pointer

dequeconst 要素へのポインターを提供します。

typedef typename Allocator::const_pointer const_pointer;

解説

const_pointer 型で要素の値を変更することはできません。 deque の要素にアクセスするには、iterator の方がより一般的に使われます。

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 を反復処理するために使用します。

反復子を宣言して使用する方法の例については、この例 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

反転された deque 内の最後の要素の次の位置を指す const 反復子を返します。

const_reverse_iterator crend() const;

戻り値

反転された deque 内の最後の要素の次の位置 (通常の順序の deque 内の最初の要素の前の位置) を指す const 逆順ランダム アクセス反復子。

解説

crend は、array::cenddeque で使用されるのと同様に、反転された 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 を初期化します。

最初の 2 つのコンストラクターは、空の初期 deque を指定し、2 番目のコンストラクターは、使用するアロケーターの型 (_Al) も指定します。

3 番目のコンストラクターは、count クラスの、指定された数 (Type) の既定値の要素を繰り返すことを指定します。

4 番目と 5 番目のコンストラクターは、値 val の (Count) 個の要素の繰り返しを指定します。

6 番目のコンストラクターは、dequeRight のコピーを指定します。

7 番目と 8 番目のコンストラクターは、deque の範囲 [First, Last) をコピーします。

7 番目のコンストラクターは、dequeRight を移動します。

8 番目のコンストラクターは、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 内の要素を参照する 2 つの反復子の違いを提供する型。

typedef typename Allocator::difference_type difference_type;

解説

difference_type は、2 つのポインターの間にある要素数と言い換えることもできます。

// 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 が空の場合は truedeque が空でない場合は 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 オブジェクトを変更できます。

_ITERATOR_DEBUG_LEVEL を 1 または 2 に定義してコンパイルすると、空の 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++ における高度な作業の 1 つです。

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

戻り値

最初の 2 つの 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_reference に割り当てられる場合、deque オブジェクトを変更できません。 operator[] の戻り値が reference に割り当てられる場合、deque オブジェクトを変更できます。

_ITERATOR_DEBUG_LEVEL を 1 または 2 に定義して使用してコンパイルすると、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 に要素をコピーします。 2 番目のオーバーライドは、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 は要素の値の変更に使用できます。 deque の要素にアクセスするには、iterator の方がより一般的に使われます。

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 は、begindeque で使用されるのと同様に、反転された 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 は、enddeque で使用されるのと同様に、反転された 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_Newsize のサイズになるまで、deque の末尾に近い要素から順に削除されます。

deque の現在のサイズが _Newsize と同じ場合は、何も実行されません。

sizedeque の現在のサイズを反映します。

// 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_fitdeque によって使われるストレージを削減するかどうかを特定するポータブルな方法はありません。

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

2 つの 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++ 標準ライブラリ リファレンス