名前空間
変種
操作

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

From cppreference.com
< cpp‎ | string‎ | basic string
 
 
 
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(/* see below */);
(5) (C++17以降)
(C++20 以降 constexpr)

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

1) str のいずれかの文字と一致する最初の文字を検索します。
2) 範囲 [s,  s + count) のいずれかの文字と一致する最初の文字を検索します。この範囲にはヌル文字が含まれる場合があります。
範囲[ss + count)有効な範囲でない場合、動作は未定義です。
3) s が指す文字文字列のいずれかの文字と一致する最初の文字を検索します。文字列の長さは、最初のヌル文字によって Traits::length(s) として決定されます。
範囲[ss + Traits::length(s))有効な範囲でない場合、動作は未定義です。
4) ch と一致する最初の文字を検索します。
5) t を、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 - 検索対象の文字文字列を指すポインタ
文字 - 検索する文字
t - std::basic_string_view に変換可能なオブジェクトで、検索対象の文字を識別するもの

[編集] 戻り値

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

[編集] 例外

1,4) 何も投げません。
5)
noexcept 指定:  
noexcept(std::is_nothrow_convertible_v<const T&, std::basic_string_view<CharT, Traits>>)

何らかの理由で例外がスローされた場合、この関数は効果がありません(強力な例外安全保証)。

[編集] 注釈

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);
    // no match
    assert(sz == std::string::npos);
 
    // (2)
    sz = "consteval"s.find_first_of("xyzabc", 0, 3);
    // no match (× are not targets)     ×××
    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) [編集]
文字が最初に現れる位置を見つける
(std::basic_string_view<CharT,Traits> の public メンバ関数) [編集]
以下から成る最大の初期セグメントの長さを返す
別のバイト文字列に含まれる文字のみ
(関数) [編集]
English 日本語 中文(简体) 中文(繁體)