次の方法で共有


stack::top

スタックの最上位の要素への参照を返します。

reference top( );
const_reference top( ) const;

戻り値

スタックの最上位のコンテナー内の最後の要素への参照。

解説

スタックがメンバー関数を適用する空にすることはできません。 スタックの最上位に最近追加された要素に位置することで、コンテナーの端に最後の要素です。

の戻り値が const_referenceに割り当てると、スタック オブジェクトは変更できません。 の戻り値が リファレンスに割り当てると、スタック オブジェクトを変更できます。

使用例

// stack_top.cpp
// compile with: /EHsc
#include <stack>
#include <iostream>

int main( )
{
   using namespace std;
   stack <int> s1;
   
   s1.push( 1 );
   s1.push( 2 );

   int& i = s1.top( );
   const int& ii = s1.top( );

   cout << "The top integer of the stack s1 is "
        << i << "." << endl;
   i--;
   cout << "The next integer down is "<< ii << "." << endl;
}
  

必要条件

ヘッダー: <スタック>

名前空間: std

参照

関連項目

stack クラス

stack::top と stack::empty

標準テンプレート ライブラリ