とくにあぶなくないRiSKのブログ

危ないRiSKのブログだったかもしれない。本当はRiSKだけどググラビリティとか取得できるIDの都合でsscriskも使ったり。

std::queue 待ち行列コンテナアダプタ

STL―標準テンプレートライブラリによるC++プログラミング 第2版

// P193 ex09-02.cpp
#include<iostream>
#include<queue>
#include<list>
int main(){
 using std::cout;
 using std::endl;
 using std::queue;
 using std::list;
 cout << "Illustrating the queue adaptor." << endl;
 const int data[] = {45, 34, 56, 27, 71, 50, 62};
 queue<int, list<int>> q;
 cout << "The queue size is now " << q.size() << endl;
 cout << "Pushing 4 elements " << endl;
 for(int i = 0; i < 4; ++i)q.push(data[i]);
 cout << "The queue size is now " << q.size() << endl;
 cout << "Popping 3 elements " << endl;
 for(int i = 0; i < 3; ++i){
  cout << q.front() << endl;
  q.pop();
 }
 cout << "The queue size is now " << q.size() << endl;
 cout << "Pushing 3 elements " << endl;
 for(int i = 4; i < 7; ++i)q.push(data[i]);
 cout << "The queue size is now " << q.size() << endl;
 cout << "Popping all elements " << endl;
 while(!q.empty()){
  cout << q.front() << endl;
  q.pop();
 }
 cout << "The queue size is now " << q.size() << endl;
}

出力:

Illustrating the queue adaptor.
The queue size is now 0
Pushing 4 elements
The queue size is now 4
Popping 3 elements
45
34
56
The queue size is now 1
Pushing 3 elements
The queue size is now 4
Popping all elements
27
71
50
62
The queue size is now 0