GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • cpprefjp - C++日本語リファレンス
  • GitHub Project

This view

  • Original page
  • New address

GeezerVison is showing cpprefjp.github.io — std::stack::top - cpprefjp C++日本語リファレンス

  1. リファレンス
  2. stack
  3. stack
  4. top

最終更新日時: 2026年08月25日 12時11分17秒 (JST)
Akira Takahashi が更新

履歴 編集

function
<stack>

std::stack::top

reference top();           // (1) C++98
constexpr reference top(); // (1) C++26

const_reference top() const;           // (2) C++98
constexpr const_reference top() const; // (2) C++26

概要

stack の次の要素への参照を返す。

これは一番最後に stack に挿入された要素で、pop() メンバ関数で削除される要素である。内部のコンテナの back() メンバ関数を呼ぶ。

効果

return c.back();

戻り値

stack の最後の要素への参照。

例

#include <iostream>
#include <stack>

int main()
{
  std::stack<int> st;

  // 要素の追加
  st.push(1);
  st.push(2);
  st.push(3);

  // 末尾要素を表示
  std::cout << st.top();
}

出力

3

実装例

reference top() { return c.back(); }
const_reference top() const { return c.back(); }

関連項目

名前 説明
push 要素の追加
pop 要素の削除

参照

  • P3372R3 constexpr containers and adaptors
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.