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

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

std::stack スタックコンテナアダプタ

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

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

出力:

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