std::stack<T,Container>::top
From cppreference.com
| reference top(); |
(1) | |
| const_reference top() const; |
(2) | |
スタックのトップ要素への参照を返します。これは最後に追加された要素です。この要素はpop()の呼び出しで削除されます。次のコードと同等です:c.back()。
目次 |
[編集] パラメータ
(なし)
[編集] 戻り値
最後の要素への参照。
[編集] 計算量
定数。
[編集] 例
このコードを実行
#include <iostream> #include <stack> void reportStackSize(const std::stack<int>& s) { std::cout << s.size() << " elements on stack\n"; } void reportStackTop(const std::stack<int>& s) { // Leaves element on stack std::cout << "Top element: " << s.top() << '\n'; } int main() { std::stack<int> s; s.push(2); s.push(6); s.push(51); reportStackSize(s); reportStackTop(s); reportStackSize(s); s.pop(); reportStackSize(s); reportStackTop(s); }
出力
3 elements on stack Top element: 51 3 elements on stack 2 elements on stack Top element: 6
[編集] 関連項目
| 要素をトップに挿入する (public member function) | |
| トップ要素を削除する (public member function) |