名前空間
変種
操作

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

提供: cppreference.com
< cpp‎ | string‎ | basic string
 
 
 
std::basic_string
 
(1)
size_type find_first_of( const basic_string& str, size_type pos = 0 ) const;
(C++11未満)
size_type find_first_of( const basic_string& str,
                         size_type pos = 0 ) const noexcept;
(C++11以上)
(C++20未満)
constexpr size_type find_first_of( const basic_string& str,
                                   size_type pos = 0 ) const noexcept;
(C++20以上)
(2)
size_type find_first_of( const CharT* s, size_type pos, size_type count ) const;
(C++20未満)
constexpr size_type find_first_of( const CharT* s,
                                   size_type pos, size_type count ) const;
(C++20以上)
(3)
size_type find_first_of( const CharT* s, size_type pos = 0 ) const;
(C++20未満)
constexpr size_type find_first_of( const CharT* s, size_type pos = 0 ) const;
(C++20以上)
(4)
size_type find_first_of( CharT ch, size_type pos = 0 ) const;
(C++11未満)
size_type find_first_of( CharT ch, size_type pos = 0 ) const noexcept;
(C++11以上)
(C++20未満)
constexpr size_type find_first_of( CharT ch, size_type pos = 0 ) const noexcept;
(C++20以上)
(5)
template < class T >

size_type
    find_first_of( const T& t,

                   size_type pos = 0 ) const noexcept(/* see below */);
(C++17以上)
(C++20未満)
template < class T >

constexpr size_type
    find_first_of( const T& t,

                   size_type pos = 0 ) const noexcept(/* see below */);
(C++20以上)

指定された文字シーケンス内の文字のいずれかと等しい最初の文字を探します。 検索は区�� [pos, size()) のみが考慮されます。 区間内に文字がなければ、 npos が返されます。

1) str 内の文字のいずれかと等しい最初の文字を探します。
2) 範囲 [s, s+count) 内の文字のいずれか等しい最初の文字を探します。 この範囲はヌル文字を含むことができます。
3) s の指す文字���内の文字のいずれかと等しい最初の文字を探します。 文字列の長さは Traits::length(s) を用いて最初のヌル文字によって決定されます。
4) ch と等しい最初の文字を探します。
5) std::basic_string_view<CharT, Traits> sv = t; によって行われたかのように、 t を文字列ビュー sv に暗黙に変換し、 sv 内の文字のいずれかと等しい最初の文字を探します。 このオーバーロードは、std::is_convertible_v<const T&, std::basic_string_view<CharT, Traits>>true であり、 std::is_convertible_v<const T&, const CharT*>false である場合にのみ、オーバーロード解決に参加します。

目次

[編集] 引数

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

[編集] 戻り値

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

[編集] 例外

5)
noexcept 指定:  
noexcept(std::is_nothrow_convertible_v<const T&, std::basic_string_view<CharT, Traits>>)

[編集] ノート

比較を行うために Traits::eq() が使用されます。

[編集]

#include <iostream>
#include <string>
 
int main()
{
    // テスト文字列
    std::string str = std::string("Hello World!");
 
    // 検索する文字列
    std::string search_str = std::string("o");
    const char* search_cstr = "Good Bye!";
 
    std::cout << str.find_first_of(search_str) << '\n';
    std::cout << str.find_first_of(search_str, 5) << '\n';
    std::cout << str.find_first_of(search_cstr) << '\n';
    std::cout << str.find_first_of(search_cstr, 0, 4) << '\n';
    // 'x' は "Hello World" の中にないため、 std::string::npos が返されます。
    std::cout << str.find_first_of('x') << '\n';
}

出力例:

4
7
1
4
18446744073709551615

[編集] 欠陥報告

以下の動作変更欠陥報告は以前に発行された C++ 標準に遡って適用されました。

DR 適用先 発行時の動作 正しい動作
LWG 2946 C++17 string_view overload causes ambiguity in some cases avoided by making it a template

[編集] 関連項目

文字列内の文字を探します
(パブリックメンバ関数) [edit]
部分文字列が現れる最後の位置を探します
(パブリックメンバ関数) [edit]
文字が現れない最初の位置を探します
(パブリックメンバ関数) [edit]
文字が現れる最後の位置を探します
(パブリックメンバ関数) [edit]
文字が現れない最後の位置を探します
(パブリックメンバ関数) [edit]
別のバイト文字列に含まれる文字のみで構成される最も長い先頭部分の長さを返します
(関数) [edit]