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

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

std::priority_queue 優先順位付き待ち行列コンテナアダプタ

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

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

出力:

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