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

履歴 編集

function
<string_view>

std::basic_string_view::contains(C++23)

constexpr bool contains(basic_string_view x) const noexcept; // (1) C++23
constexpr bool contains(charT x) const noexcept;             // (2) C++23
constexpr bool contains(const charT* x) const;               // (3) C++23

概要

指定の文字・文字列が含まれているかを判定する。

  • (1) : *thisが参照する文字列範囲に、xが参照する文字列範囲を含んでいるかを判定する
  • (2) : *thisが参照する文字列範囲に、文字xが含まれているかを判定する
  • (3) : *thisが参照する文字列範囲に、文字列xが含まれているかを判定する

この関数は、find(x) != nposの代わりに使用できる。

戻り値

  • 以下と等価である
    return find(x) != npos;
    

例外

  • (1), (2) : 投げない

#include <cassert>
#include <string_view>

int main() {
  std::string_view sv = "Hello World";

  // (1)
  {
    std::string_view target = "rl";
    assert(sv.contains(target));
  }

  // (2)
  {
    assert(sv.contains('W'));
  }

  // (3)
  {
    assert(sv.contains("rl"));
  }
}

出力

バージョン

言語

  • C++20

処理系

参照