次のサンプル コードは、Visual C++ で STL 関数の queue::push
、 queue::pop
、 queue::empty
、 queue::back
、 queue::front
、 queue::size
を使用する方法を示しています。 この記事の情報は、アンマネージ Visual C++ コードにのみ適用されます。
元の製品バージョン: Visual C++
元の KB 番号: 157622
まとめ
queue
アダプターは、queue
でサポートされているコンテナーの種類によって定義された型のオブジェクトを保持します。 サポートされている 2 つのコンテナーは、 list
と deque
です。 オブジェクトは push()
によって挿入され、 pop()
によって削除されます。 front()
は、 queue
の最も古い項目 (FIFO とも呼ばれます) を返し、 back()
は queue
に挿入された最新の項目を返します。
必須ヘッダー
<queue>
プロトタイプ
queue::push();
queue::pop();
queue::empty();
queue::back();
queue::front();
queue::size();
Note
プロトタイプのクラス名またはパラメーター名が、ヘッダー ファイルのバージョンと一致しない場合があります。 読みやすさを向上させるために変更されたものもあります。
サンプル コード
このサンプルでは、 list
コンテナーと deque
コンテナーを使用したキューの実装を示します。
//////////////////////////////////////////////////////////////////////
// Compile options needed: none
// <filename> : queue.cpp
// Functions:
// queue::push(), queue::pop(), queue::empty(), queue::back(),
// queue::front(),queue::size()
// Copyright (c) 1996 Microsoft Corporation. All rights reserved.
//////////////////////////////////////////////////////////////////////
/* Compile options needed: /GX */
#include <list>
#include <iostream>
#include <queue>
#include <deque>
using namespace std;
#if _MSC_VER > 1020 // if VC++ version is > 4.2
using namespace std; // std c++ libs implemented in std
#endif
// Using queue with list
typedef list<int, allocator<int>> INTLIST;
typedef queue<int, INTLIST> INTQUEUE;
// Using queue with deque
typedef deque<char *, allocator<char *>> CHARDEQUE;
typedef queue<char *, CHARDEQUE> CHARQUEUE;
void main(void)
{
int size_q;
INTQUEUE q;
CHARQUEUE p;
// Insert items in the queue(uses list)
q.push(42);
q.push(100);
q.push(49);
q.push(201);
// Output the item inserted last using back()
cout << q.back() << endl;
// Output the size of queue
size_q = q.size();
cout << "size of q is:" << size_q << endl;
// Output items in queue using front()
// and use pop() to get to next item until
// queue is empty
while (!q.empty())
{
cout << q.front() << endl;
q.pop();
}
// Insert items in the queue(uses deque)
p.push("cat");
p.push("ape");
p.push("dog");
p.push("mouse");
p.push("horse");
// Output the item inserted last using back()
cout << p.back() << endl;
// Output the size of queue
size_q = p.size();
cout << "size of p is:" << size_q << endl;
// Output items in queue using front()
// and use pop() to get to next item until
// queue is empty
while (!p.empty())
{
cout << p.front() << endl;
p.pop();
}
}
プログラムの出力
201
size of q is:4
42
100
49
201
horse
size of p is:5
cat
ape
dog
mouse
horse
関連情報
STL queue
クラスのメンバー関数と同じ情報については、 queueを参照してください。