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

  1. リファレンス
  2. string
  3. basic_string
  4. contains

最終更新日時: 2026年07月22日 16時08分23秒 (JST)
rotarymars が更新

履歴 編集

function
<string>

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

constexpr bool contains(basic_string_view<charT, traits> 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 basic_string_view<charT, traits>(data(), size()).contains(x);
    

例外

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

例

#include <cassert>
#include <string>
#include <string_view>

int main() {
  std::string s = "Hello World";

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

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

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

出力

バージョン

言語

  • C++23

処理系

  • Clang: 12.0 ✅
  • GCC: 11 ✅
  • Visual C++: 2022 ✅

参照

  • P1679R3 string contains function
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.