Namespaces
Variants

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

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

指定された文字シーケンスと等しい最初の部分文字列を検索します。

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

パラメータ

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

戻り値

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

計算量

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

例

#include <string_view>
int main()
{
    using namespace std::literals;
    constexpr auto str{" long long int;"sv};
    static_assert(
        1 == str.find("long"sv)            && "<- find(v , pos = 0)" &&
        6 == str.find("long"sv, 2)         && "<- find(v , pos = 2)" &&
        0 == str.find(' ')                 && "<- find(ch, pos = 0)" &&
        2 == str.find('o', 1)              && "<- find(ch, pos = 1)" &&
        2 == str.find("on")                && "<- find(s , pos = 0)" &&
        6 == str.find("long double", 5, 4) && "<- find(s , pos = 5, count = 4)"
    );
    static_assert(str.npos == str.find("float"));
}

関連項目

部分文字列の最後の出現を検索
(公開メンバ関数)
文字の最初の出現を検索
(公開メンバ関数)
文字の最後の出現を検索
(公開メンバ関数)
文字の最初の不在を検索
(公開メンバ関数)
文字の最後の不在を検索
(公開メンバ関数)
指定された部分文字列の最初の出現を検索
(std::basic_string<CharT,Traits,Allocator>の公開メンバ関数)