Namespaces
Variants

std::basic_string_view<CharT,Traits>::find_first_of

- ja.cppreference.net より
 
 
 
 
constexpr size_type
    find_first_of( basic_string_view v, size_type pos = 0 ) const noexcept;
(1) (C++17 から)
constexpr size_type
    find_first_of( CharT ch, size_type pos = 0 ) const noexcept;
(2) (C++17 から)
constexpr size_type
    find_first_of( const CharT* s, size_type pos, size_type count ) const;
(3) (C++17 から)
constexpr size_type
    find_first_of( const CharT* s, size_type pos = 0 ) const;
(4) (C++17 から)

指定された文字シーケンス内のいずれかの文字と等しい最初の文字を探します。

1) このビューにおいて、位置 v から開始して、pos のいずれかの文字が最初に現れる位置を探します。
2) find_first_of(basic_string_view(std::addressof(ch), 1), pos) と同等です。
3) find_first_of(basic_string_view(s, count), pos) と同等です。
4) find_first_of(basic_string_view(s), pos) と同等です。

パラメータ

v - 検索対象のビュー
pos - 検索を開始する位置
count - 検索対象の文字列の長さ
s - 検索対象の文字列へのポインタ
ch - 検索対象の文字

戻り値

部分文字列のいずれかの文字が最初に出現する位置、または npos そのような文字が見つからない場合。

計算量

O( size() * v. size() ) 最悪の場合。

例

#include <string_view>
using namespace std::literals;
constexpr auto N = std::string_view::npos;
constexpr bool is_white_space(const char c)
{
    return " \t\n\f\r\v"sv.find_first_of(c) != N;
};
static_assert(
    1 == "alignas"sv.find_first_of("klmn"sv) &&
      //   └─────────────────────────┘
    N == "alignof"sv.find_first_of("wxyz"sv) &&
      //
    3 == "concept"sv.find_first_of("bcde"sv, /* pos= */ 1) &&
      //     └───────────────────────┘
    N == "consteval"sv.find_first_of("oxyz"sv, /* pos= */ 2) &&
      //
    6 == "constexpr"sv.find_first_of('x') &&
      //        └─────────────────────┘
    N == "constinit"sv.find_first_of('x') &&
      //
    6 == "const_cast"sv.find_first_of('c', /* pos= */ 4) &&
      //        └──────────────────────┘
    N == "continue"sv.find_first_of('c', /* pos= */ 42) &&
      //
    5 == "co_await"sv.find_first_of("cba", /* pos= */ 4) &&
      //       └───────────────────────┘
    7 == "decltype"sv.find_first_of("def", /* pos= */ 2, /* count= */ 2) &&
      //         └────────────────────┘
    N == "decltype"sv.find_first_of("def", /* pos= */ 2, /* count= */ 1) &&
      //
    is_white_space(' ') && is_white_space('\r') && !is_white_space('\a')
);
int main() {}

関連項目

ビュー内の文字を検索
(公開メンバ関数)
部分文字列の最後の出現を検索
(公開メンバ関数)
文字の最後の出現を検索
(公開メンバ関数)
文字の最初の不在を検索
(公開メンバ関数)
文字の最後の不在を検索
(公開メンバ関数)
文字の最初の出現を検索
(std::basic_string<CharT,Traits,Allocator>の公開メンバ関数)