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

出自cppreference.com
 
 
 
 
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) 在此視圖中尋找 v 的最後一次出現,從位置 pos 開始。
2) 等價於 rfind(basic_string_view(std::addressof(c), 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> 的公開成員函數) [編輯]