std::stack
stack†
LIFO(Last In First Out)方式のスタックとして値を格納する.
dequeを基底としたコンテナアダプタ.
クラスのテンプレート仕様は,
template < class T, class Container = deque<T> > class stack;
となっており,
定義時にデータ型,基底コンテナ(デフォルトはdeque)を指定することができる.
インクルード†
#include <stack>
初期化(コンストラクタ)†
explicit stack(const Container& ctnr = Container());
メンバ関数†
- top() : スタックのトップ(コンテナの末尾)の要素への参照を返す関数
value_type& top();
const value_type& top() const;
- size() : コンテナのサイズを返す関数
size_type size() const;
- empty() : コンテナが空だったらtrueを返す関数.
bool empty () const;
- push(val) : スタックに要素を追加(コンテナの末尾に追加)
void push(const T& val);
- pop() : スタックのトップ(コンテナの末尾)の要素を削除
void pop();
使用例
#include <iostream>
#include <stack>
using namespace std;
int main(void)
{
stack<int> x;
for(int i = 0; i < 5; ++i){
x.push(i);
cout << x.top() << " ";
}
cout << endl;
for(int i = 0; i < 5; ++i){
cout << x.top() << " ";
x.pop();
}
cout << endl;
return 0;
}
実行結果
0 1 2 3 4
4 3 2 1 0