Namespaces
Variants

std::basic_string<CharT,Traits,Allocator>:: find_first_of

From cppreference.net
std::basic_string
size_type find_first_of ( const basic_string & str, size_type pos = 0 ) const ;
(1) (C++11以降noexcept)
(C++20以降constexpr)
size_type find_first_of ( const CharT * s,
size_type pos, size_type count ) const ;
(2) (C++20以降constexpr)
size_type find_first_of ( const CharT * s, size_type pos = 0 ) const ;
(3) (C++20以降constexpr)
size_type find_first_of ( CharT ch, size_type pos = 0 ) const ;
(4) (C++11以降noexcept)
(C++20以降constexpr)
template < class StringViewLike >

size_type
find_first_of ( const StringViewLike & t,

size_type pos = 0 ) const noexcept ( /* 下記参照 */ ) ;
(5) (C++17以降)
(C++20以降constexpr)

指定された文字シーケンス内のいずれかの文字と等しい最初の文字を検索します。検索は範囲 [ pos , size() ) のみを考慮します。指定された文字シーケンス内の文字が範囲内に存在しない場合、 npos が返されます。

1) str 内の文字のいずれかに等しい最初の文字を検索します。
2) 範囲 [ s , s + count ) 内の文字のいずれかに等しい最初の文字を検索します。この範囲にはnull文字を含めることができます。
[ s , s + count ) 有効な範囲 でない場合、動作は未定義です。
3) 文字列 s が指す文字列内のいずれかの文字と等しい最初の文字を検索します。文字列の長さは、 Traits :: length ( s ) を使用して最初のヌル文字によって決定されます。
[ s , s + Traits :: length ( s ) ) 有効な範囲 でない場合、動作は未定義です。
4) 最初の文字が ch と等しい文字を検索します。
5) t を文字列ビュー sv に暗黙的に変換する( std:: basic_string_view < CharT, Traits > sv = t ; によって行われるかのように)。その後、 sv 内の文字のいずれかに等しい最初の文字を検索する。
このオーバーロードは、以下の条件が満たされる場合にのみオーバーロード解決に参加します: std:: is_convertible_v < const StringViewLike & ,
std:: basic_string_view < CharT, Traits >>
true であり、かつ std:: is_convertible_v < const StringViewLike & , const CharT * > false である場合です。

目次

パラメータ

str - 検索対象の文字列を識別する文字列
pos - 検索を開始する位置
count - 検索対象の文字列を識別する文字列の長さ
s - 検索対象の文字列を識別する文字列へのポインタ
ch - 検索対象の文字
t - 検索対象の文字列を識別するオブジェクト( std::basic_string_view に変換可能)

戻り値

見つかった文字の位置、または npos そのような文字が見つからない場合。

例外

1,4) 例外を送出しない。
5)
noexcept 指定子:
noexcept ( std:: is_nothrow_convertible_v < const T & , std:: basic_string_view < CharT, Traits >> )

何らかの理由で例外がスローされた場合、この関数は何も効果を持ちません( strong exception safety guarantee )。

注記

Traits :: eq ( ) は比較を実行するために使用されます。

#include <cassert>
#include <iostream>
#include <string>
#include <string_view>
int main()
{
    using namespace std::literals;
    std::string::size_type sz;
    // (1)
    sz = "alignas"s.find_first_of("klmn"s);
    //     └────────────────────────┘
    assert(sz == 1);
    sz = "alignof"s.find_first_of("wxyz"s);
    // 一致なし
    assert(sz == std::string::npos);
    // (2)
    sz = "consteval"s.find_first_of("xyzabc", 0, 3);
    // 一致なし (× は対象外)     ×××
    assert(sz == std::string::npos);
    sz = "consteval"s.find_first_of("xyzabc", 0, 6);
    //    └───────────────────────────────┘
    assert(sz == 0);
    // (3)
    sz = "decltype"s.find_first_of("xyzabc");
    //      └────────────────────────────┘
    assert(sz == 2);
    // (4)
    sz = "co_await"s.find_first_of('a');
    //       └──────────────────────┘
    assert(sz == 3);
    // (5)
    sz = "constinit"s.find_first_of("int"sv);
    //      └─────────────────────────┘
    assert(sz == 2);
    std::cout << "All tests passed.\n";
}

出力:

All tests passed.

不具合報告

以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。

DR 適用対象 公開時の動作 正しい動作
LWG 847 C++98 例外安全性保証が存在しなかった 強い例外安全性保証を追加
LWG 2064 C++11 オーバーロード (3,4) がnoexcept指定されていた 削除
LWG 2946 C++17 オーバーロード (5) が一部の場合に曖昧性を引き起こした テンプレート化することで回避
P1148R0 C++11
C++17
オーバーロード (4,5) のnoexcept指定が
LWG2064/LWG2946により誤って削除されていた
復元

関連項目

指定された部分文字列の最初の出現を検索する
(public member function)
部分文字列の最後の出現を検索する
(public member function)
文字が含まれない最初の位置を検索する
(public member function)
文字の最後の出現を検索する
(public member function)
文字が含まれない最後の位置を検索する
(public member function)
文字の最初の出現を検索する
(public member function of std::basic_string_view<CharT,Traits> )
別のバイト文字列に含まれる文字のみで構成される
最大の先頭セグメントの長さを返す
(function)