queue クラス

基になるコンテナー型のいくつかについて、一番前と一番後ろの要素へのアクセスを制限することで機能を限定するテンプレート コンテナー アダプター クラスです。 要素は一番後ろに追加するか、一番前から削除することができ、queue のどちらの端でも要素を検査することができます。

構文

template <class Type, class Container = deque <Type>>
class queue

パラメーター

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

Container
queue を実装するために使用する基になるコンテナーの型。

解説

queue オブジェクトの最初のテンプレート パラメーターで指定するクラス Type の要素は value_type と同義で、2 番目のテンプレート パラメーターで指定する、基になるコンテナー クラス Container 内の要素の型と一致する必要があります。 対象の型のオブジェクトをコピーし、対象の型の変数に値を割り当てられるように、Type は割り当て可能でなければいけません。

queue に適した基になるコンテナー クラスには、dequelist、または frontbackpush_backpop_front の操作をサポートするその他のすべてのシーケンス コンテナーが含まれます。 基になるコンテナー クラスは、コンテナー アダプター内にカプセル化されます。コンテナー アダプターは、限られた一連のシーケンス コンテナーのメンバーの関数のみをパブリック インターフェイスとして公開します。

queue オブジェクトは、クラス Type の要素が等価比較できる場合にのみ等価比較でき、クラス Type の要素が小なり比較できる場合にのみ小なり比較できます。

C++ 標準ライブラリで定義されたコンテナー アダプターには、stackqueuepriority_queue の 3 つの種類があります。 それぞれが、基になるコンテナー クラスの機能を制限して、標準的なデータ構造に対して精密に制御されたインターフェイスを提供します。

  • stack クラスは、後入れ先出し (LIFO) のデータ構造をサポートしています。 思い描くのに助けとなるのは、積み重ねられた皿です。 要素 (皿) は、積み重ねの一番上からのみ挿入、検査、または削除できます。積み重ねの一番上に相当するのは、基本のコンテナーの末尾にある最後の要素です。 一番上の要素にのみアクセスできる制限があることが、stack クラスを使用する理由です。

  • queue クラスは、先入れ先出し (FIFO) のデータ構造をサポートしています。 思い描くのに助けとなるのは、銀行の窓口で並んでいる人です。 要素 (人々) は、列の一番後ろに追加され、列の一番前から取り除くことができます。 列の一番前と一番後ろの両方を検査できます。 このように frontback の要素にのみアクセスできる制限があることが、queue クラスを使用する理由です。

  • priority_queue クラスは、最も大きな要素が常に先頭の位置になるように、その要素を並べ替えます。 要素の挿入、および先頭の要素の検査と削除をサポートしています。 思い描くのに助けとなるのは、年齢、身長、またはその他の条件によって整列している人です。

メンバー

コンストラクター

名前 説明
queue 空であるか、基本のコンテナー オブジェクトのコピーである queue を構築します。

Typedefs

名前 説明
container_type queue によって適合されるように、基本のコンテナーを提供する型。
size_type queue 内の要素の数を表すことができる符号なし整数型。
value_type queue 内に要素として格納されるオブジェクトの種類を表す型。

関数

名前 説明
back queue の後ろに最も直近に追加された要素への参照を返します。
empty queue が空かどうかをテストします。
front queue の一番前にある最初の要素への参照を返します。
pop queue の一番前から要素を削除します。
push queue の後ろに要素を追加します。
size queue 内の要素数を返します。

back

queue の後ろに最も直近に追加された要素への参照を返します。

reference back();

const_reference back() const;

戻り値

queue の最後の要素。 queue が空の場合、戻り値は定義されません。

解説

back の戻り値が const_reference に割り当てられる場合、queue オブジェクトを変更できません。 back の戻り値が reference に割り当てられる場合、queue オブジェクトを変更できます。

_ITERATOR_DEBUG_LEVEL を 1 または 2 に定義してコンパイルすると、空の queue 内の要素にアクセスしようとした場合に実行時エラーが発生します。 詳しくは、「チェックを行う反復子」をご覧ください。

// queue_back.cpp
// compile with: /EHsc
#include <queue>
#include <iostream>

int main( )
{
   using namespace std;
   queue <int> q1;

   q1.push( 10 );
   q1.push( 11 );

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

   cout << "The integer at the back of queue q1 is " << i
        << "." << endl;
   cout << "The integer at the front of queue q1 is " << ii
        << "." << endl;
}

container_type

適合されるように、基本のコンテナーを提供する型。

typedef Container container_type;

解説

この型は、テンプレート パラメーター Containerのシノニムです。 C++ 標準ライブラリのシーケンス コンテナー クラスである list クラスと既定の deque クラスは、queue オブジェクトの基本のコンテナーとして使用するための要件を満たしています。 要件を満たすユーザー定義型を使用することもできます。

Container の詳細については、queue Class のトピックのコメントのセクションをご覧ください。

container_type の宣言や使用の方法の例については、queue の例を参照してください。

empty

queue が空かどうかをテストします。

bool empty() const;

戻り値

queue が空の場合は truequeue が空でない場合は false

// queue_empty.cpp
// compile with: /EHsc
#include <queue>
#include <iostream>

int main( )
{
using namespace std;

   // Declares queues with default deque base container
   queue <int> q1, q2;

   q1.push( 1 );

   if ( q1.empty( ) )
      cout << "The queue q1 is empty." << endl;
   else
      cout << "The queue q1 is not empty." << endl;

   if ( q2.empty( ) )
      cout << "The queue q2 is empty." << endl;
   else
      cout << "The queue q2 is not empty." << endl;
}
The queue q1 is not empty.
The queue q2 is empty.

front

queue の一番前にある最初の要素への参照を返します。

reference front();

const_reference front() const;

戻り値

queue の最初の要素。 queue が空の場合、戻り値は定義されません。

解説

front の戻り値が const_reference に割り当てられる場合、queue オブジェクトを変更できません。 front の戻り値が reference に割り当てられる場合、queue オブジェクトを変更できます。

このメンバー関数は、被制御シーケンスの最初の要素への reference を返します。被制御シーケンスを空にすることはできません。

_ITERATOR_DEBUG_LEVEL を 1 または 2 に定義してコンパイルすると、空の queue 内の要素にアクセスしようとした場合に実行時エラーが発生します。 詳しくは、「チェックを行う反復子」をご覧ください。

// queue_front.cpp
// compile with: /EHsc
#include <queue>
#include <iostream>

int main() {
   using namespace std;
   queue <int> q1;

   q1.push( 10 );
   q1.push( 20 );
   q1.push( 30 );

   queue <int>::size_type i;
   i = q1.size( );
   cout << "The queue length is " << i << "." << endl;

   int& ii = q1.back( );
   int& iii = q1.front( );

   cout << "The integer at the back of queue q1 is " << ii
        << "." << endl;
   cout << "The integer at the front of queue q1 is " << iii
        << "." << endl;
}

pop

queue の一番前から要素を削除します。

void pop();

解説

メンバー関数を適用するには、queue は空でない必要があります。 queue の先頭は最も直近に追加された要素によって占有されている位置で、コンテナーの末尾にある最後の要素です。

// queue_pop.cpp
// compile with: /EHsc
#include <queue>
#include <iostream>

int main( )
{
   using namespace std;
   queue <int> q1, s2;

   q1.push( 10 );
   q1.push( 20 );
   q1.push( 30 );

   queue <int>::size_type i;
   i = q1.size( );
   cout << "The queue length is " << i << "." << endl;

   i = q1.front( );
   cout << "The element at the front of the queue is "
        << i << "." << endl;

   q1.pop( );

   i = q1.size( );
   cout << "After a pop the queue length is "
        << i << "." << endl;

   i = q1. front ( );
   cout << "After a pop, the element at the front of the queue is "
        << i << "." << endl;
}
The queue length is 3.
The element at the front of the queue is 10.
After a pop the queue length is 2.
After a pop, the element at the front of the queue is 20.

push

queue の後ろに要素を追加します。

void push(const Type& val);

パラメーター

val
queue の一番後ろに追加する要素。

解説

queue の一番後ろは最も直近に追加された要素によって占有されている位置で、コンテナーの末尾にある最後の要素です。

// queue_push.cpp
// compile with: /EHsc
#include <queue>
#include <iostream>

int main( )
{
   using namespace std;
   queue <int> q1;

   q1.push( 10 );
   q1.push( 20 );
   q1.push( 30 );

   queue <int>::size_type i;
   i = q1.size( );
   cout << "The queue length is " << i << "." << endl;

   i = q1.front( );
   cout << "The element at the front of the queue is "
        << i << "." << endl;
}
The queue length is 3.
The element at the front of the queue is 10.

queue

空であるか、基本のコンテナー オブジェクトのコピーである queue を構築します。

queue();

explicit queue(const container_type& right);

パラメーター

right
構築される queue のコピー元となる const コンテナー。

解説

queue の既定の基本コンテナーは deque です。 基本のコンテナーとして list を指定することもできますが、vector を指定することはできません。これは、必要な pop_front メンバー関数がないためです。

// queue_queue.cpp
// compile with: /EHsc
#include <queue>
#include <vector>
#include <list>
#include <iostream>

int main( )
{
   using namespace std;

   // Declares queue with default deque base container
   queue <char> q1;

   // Explicitly declares a queue with deque base container
   queue <char, deque<char> > q2;

   // These lines don't cause an error, even though they
   // declares a queue with a vector base container
   queue <int, vector<int> > q3;
   q3.push( 10 );
   // but the following would cause an error because vector has
   // no pop_front member function
   // q3.pop( );

   // Declares a queue with list base container
   queue <int, list<int> > q4;

   // The second member function copies elements from a container
   list<int> li1;
   li1.push_back( 1 );
   li1.push_back( 2 );
   queue <int, list<int> > q5( li1 );
   cout << "The element at the front of queue q5 is "
        << q5.front( ) << "." << endl;
   cout << "The element at the back of queue q5 is "
        << q5.back( ) << "." << endl;
}
The element at the front of queue q5 is 1.
The element at the back of queue q5 is 2.

size

queue 内の要素数を返します。

size_type size() const;

戻り値

queue の現在の長さ。

// queue_size.cpp
// compile with: /EHsc
#include <queue>
#include <iostream>

int main( )
{
   using namespace std;
   queue <int> q1, q2;
   queue <int>::size_type i;

   q1.push( 1 );
   i = q1.size( );
   cout << "The queue length is " << i << "." << endl;

   q1.push( 2 );
   i = q1.size( );
   cout << "The queue length is now " << i << "." << endl;
}
The queue length is 1.
The queue length is now 2.

size_type

queue 内の要素の数を表すことができる符号なし整数型。

typedef typename Container::size_type size_type;

解説

この型は、queue によって採用された基本コンテナーの size_type のシノニムです。

size_type の宣言や使用の方法の例については、queue::front の例を参照してください。

value_type

queue 内に要素として格納されるオブジェクトの種類を表す型。

typedef typename Container::value_type value_type;

解説

この型は、queue によって採用された基本コンテナーの value_type のシノニムです。

// queue_value_type.cpp
// compile with: /EHsc
#include <queue>
#include <iostream>

int main( )
{
using namespace std;

   // Declares queues with default deque base container
   queue<int>::value_type AnInt;

   AnInt = 69;
   cout << "The value_type is AnInt = " << AnInt << endl;

   queue<int> q1;
   q1.push(AnInt);
   cout << "The element at the front of the queue is "
        << q1.front( ) << "." << endl;
}
The value_type is AnInt = 69
The element at the front of the queue is 69.

関連項目

C++ 標準ライブラリ内のスレッド セーフ
C++ 標準ライブラリ リファレンス