C++容器のQueue

2405 ワード

概要
Queueは、エレメントが先進的なプリアウト操作を実行するために設計されたコンテナアダプタです.要素は容器の一端に挿入され、他端に取り出されます.
Queueはコンテナアダプタとして実装され、指定されたコンテナクラスを潜在的なコンテナとして使用してデータを格納し、操作関数のセットを提供してコンテナ内のデータを操作するクラスである.
潜在的なコンテナは、標準コンテナクラステンプレートまたは特殊に設計されたコンテナクラスであり、次の操作を行う必要があります.
Ø  front()
Ø  back()
Ø  push_back()
Ø  pop_back()
ここで、標準コンテナクラスのdequeおよびlistは、その要件を満たすことができる.デフォルトでは、そのコンテナクラスが潜在的なコンテナとして指定されていない場合、dequeはデフォルトのコンテナクラスになります.
メンバー関数
queue::queue()
queueコンテナアダプタオブジェクトを作成します.
queue::empty()
コンテナが空かどうかを判断し、空であればtrueを返し、そうでなければfalseを返します.
queue::size()
queue内の要素の数を返します.
queue::front()
キューヘッダ要素の参照を返します.
queue::back()
キューの最後の要素の参照を返します.
queue::push()
キューの最後に要素を挿入します.
queue::emplace()
キューの最後に要素を挿入し、挿入した要素はその構造関数によって構築されます.
queue::pop()
キューの先頭から要素を取り出します.
queue::swap()
2つのキューの要素を交換します.
サンプルプログラム
#include 
#include 
#include 
#include 

using namespace std;

int main(void)
{
	deque deq(3, 10);
	list lis(3, 20);
	
	// queue::queue()
	queue first;
	queue second(deq);
	queue> third;
	queue> fourth(lis);
	
	// queue::empty()
	if(first.empty()){
		cout << "Queue is empty." << endl;
	}else{
		cout << "Queue is not empty" << endl;
	}
	
	// queue::size()
	cout << "The number of data in second : ";
	cout << second.size() << endl;
	
	// queue::front()
	cout << "The first element is : ";
	cout << second.front() << endl;
	
	// queue::back()
	cout << "The list element is : ";
	cout << second.back() << endl;
	
	// queue::push()
	second.push(20);
	cout << "The list element is : ";
	cout << second.back() << endl;
	
	// queue::emplace()
	second.emplace();
	cout << "The list element is : ";
	cout << second.back() << endl;
	
	// queue::pop()
	second.pop();
	cout << "The list element is : ";
	cout << second.back() << endl;
	
	// queue::swap()
	first.swap(second);
	cout << "The size of first : " << first.size() << endl;
	cout << "The size of second : " << second.size() << endl;

	return 0;
}