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::empty - cpprefjp C++日本語リファレンス

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

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

履歴 編集

function
<stack>

std::stack::empty

bool empty() const;               // (1) C++98
[[nodiscard]] bool empty() const; // (1) C++20
constexpr bool empty() const;     // (1) C++26

概要

stack が空か否か、つまり要素数が 0 か否かを返す。

内部のコンテナの empty() メンバ関数を呼ぶ。

戻り値

要素数が 0 の場合は true、それ以外の場合は false 。

計算量

定数時間 O(1)。

例

#include <iostream>
#include <stack>

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

  // 空なら "empty", そうでなければ "not empty" と表示する
  if (st.empty()) {
    std::cout << "empty" << std::endl;
  } else {
    std::cout << "not empty" << std::endl;
  }

  // 要素を追加
  st.push(1);

  // 空なら "empty", そうでなければ "not empty" と表示する
  if (st.empty()) {
    std::cout << "empty" << std::endl;
  } else {
    std::cout << "not empty" << std::endl;
  }
}

出力

empty
not empty

実装例

bool empty() const { return c.empty(); }

関連項目

名前 説明
size 要素数を返す

参照

  • P0600R1 [[nodiscard]] in the Library
    • C++20で[[nodiscard]]が付加された
  • P2422R1 Remove nodiscard annotations from the standard library specification
    • C++26で[[nodiscard]]指定が削除された
  • P3372R3 constexpr containers and adaptors
You are visitor 01402520 — 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.