Namespaces
Variants

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

From cppreference.net
constexpr size_type rfind ( basic_string_view v, size_type pos = npos ) const noexcept ;
(1) (C++17以降)
constexpr size_type rfind ( CharT ch, size_type pos = npos ) const noexcept ;
(2) (C++17以降)
constexpr size_type rfind ( const CharT * s, size_type pos, size_type count ) const ;
(3) (C++17以降)
constexpr size_type rfind ( const CharT * s, size_type pos = npos ) const ;
(4) (C++17以降)

指定された文字シーケンスと等しい最後の部分文字列を検索します。検索は pos から始まり、右から左へ進行します(したがって、見つかった部分文字列がある場合、 pos 以降の位置から始まることはできません)。 npos または size() - 1 以上の値が pos として渡された場合、文字列全体が検索されます。

1) このビュー内で位置 pos から開始して、 v の最後の出現を検索します。
2) 次と同等: rfind ( basic_string_view ( std:: addressof ( ch ) , 1 ) , pos )
3) 次と等価: rfind ( basic_string_view ( s, count ) , pos )
4) 次と同等: rfind ( 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 N = std::string_view::npos;
    static_assert(true
        && (6 == "AB AB AB"sv.rfind("AB"))
        && (6 == "AB AB AB"sv.rfind("ABCD", N, 2))
        && (3 == "AB AB AB"sv.rfind("AB", 5))
        && (0 == "AB CD EF"sv.rfind("AB", 0))
        && (2 == "B AB AB "sv.rfind("AB", 2))
        && (N == "B AB AB "sv.rfind("AB", 1))
        && (5 == "B AB AB "sv.rfind('A'))
        && (4 == "AB AB AB"sv.rfind('B', 4))
        && (N == "AB AB AB"sv.rfind('C'))
    );
}

関連項目

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