最終更新日時(UTC):
が更新

履歴 編集

function
<regex>

std::match_results::empty(C++11)

bool empty() const;               // C++11
[[nodiscard]] bool empty() const; // C++20

概要

*this が空か否か(つまり、マッチが失敗したか否か)を返す。

戻り値

size() == 0

備考

  • regex_match、および、regex_search の引数に渡した match_results オブジェクトは、マッチが成功すると empty() != true となる。
    match_results オブジェクトの結果が利用可能か否かを確認する場合には ready を使用すると良い。
  • regex_iterator を間接参照して得られる match_results オブジェクトは、必ず empty() != true となる。
  • empty() == true の場合、match_results のメンバ関数には結果が未規定であるものがあるので、注意が必要である(regex_match、および、regex_search を参照)。
  • 本メンバ関数は ready() == false でも呼び出すことが可能である(その場合、true が返される)。

#include <iostream>
#include <regex>

int main()
{
  const char s1[] = " abc ";
  const char s2[] = " 012 ";
  const std::regex re("\\d+");

  std::cmatch m;
  // regex_search 実行前
  std::cout << std::boolalpha << "ready = " << m.ready() << ", empty = " << m.empty() << std::endl;

  // regex_search 実行後(マッチ失敗)
  if (std::regex_search(s1, m, re)) {
    std::cout << "match:ready = " << m.ready() << ", empty = " << m.empty() << std::endl;
  } else {
    std::cout << "not match:ready = " << m.ready() << ", empty = " << m.empty() << std::endl;
  }

  // regex_search 実行後(マッチ成功)
  if (std::regex_search(s2, m, re)) {
    std::cout << "match:ready = " << m.ready() << ", empty = " << m.empty() << std::endl;
  } else {
    std::cout << "not match:ready = " << m.ready() << ", empty = " << m.empty() << std::endl;
  }
}

出力

ready = false, empty = true
not match:ready = true, empty = true
match:ready = true, empty = false

バージョン

言語

  • C++11

処理系

参照