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

出自cppreference.com
 
 
 
std::basic_string
 
size_type rfind( const basic_string& str, size_type pos = npos ) const;
(1) (C++11 起為 noexcept)
(C++20 起為 constexpr)
size_type rfind( const CharT* s, size_type pos, size_type count ) const;
(2) (C++20 起為 constexpr)
size_type rfind( const CharT* s, size_type pos = npos ) const;
(3) (C++20 起為 constexpr)
size_type rfind( CharT ch, size_type pos = npos ) const;
(4) (C++11 起為 noexcept)
(C++20 起為 constexpr)
template< class StringViewLike >
size_type rfind( const StringViewLike& t,
                 size_type pos = npos ) const noexcept(/* 见下文 */);
(5) (C++17 起)
(C++20 起為 constexpr)

尋找最後一個等於給定字符序列的子串。搜索從 pos 開始並從右向左進行(因此,找到的子串不會從 pos 之後的位置開始)。如果將 npos 或任何不小於 size()- 1 的值作為 pos 傳遞,那麼就會在整個字符串中搜索。

1) 尋找最後一個等於 str 的子串。
2) 尋找最後一個等於範圍 [ss + count) 的子串。此範圍可以包含空字符。
如果 [ss + count) 不是有效範圍,那麼行為未定義。
3) 尋找最後一個等於 s 所指向的字符串的子串。該字符串的長度由首個空字符,通過 Traits::length(s) 確定。
如果 [ss + Traits::length(s)) 不是有效範圍,那麼行為未定義。
4) 尋找最後一個等於 ch 的字符。
5) 如同用 std::basic_string_view<CharT, Traits> sv = t;t 隱式轉換到字符串視圖 sv ,然後尋找最後一個等於 sv 內容的子串。
此重載只有在 std::is_convertible_v<const StringViewLike&,
std::basic_string_view<CharT, Traits>>
truestd::is_convertible_v<const StringViewLike&, const CharT*>false 時才會參與重載決議。

所有情況下均調用 Traits::eq 檢查相等性。

參數

str - 要搜索的字符串
pos - 開始搜索的位置
count - 要搜索的子串長度
s - 指向要搜索的字符串的指針
ch - 要搜索的字符
t - 要搜索的對象(可轉換到 std::basic_string_view

返回值

找到的子串的首字符位置,或者在找不到這種子串則時返回 npos。注意這是從字符串開始,而非末尾的偏移。

如果搜索的是空字符串(str.size()countTraits::length(s) 為零),那麼立即找到空字符串並返回:

  • pos,如果 pos < size()
  • 否則是 size(),包括 pos == npos 的情況。

否則,如果 size() 為零,那麼始終返回 npos

異常

1,4) 不拋出。
5)
noexcept 說明:  
noexcept(std::is_nothrow_convertible_v<const T&, std::basic_string_view<CharT, Traits>>)

如果因為任何原因拋出了異常,那麼此函數無效果(強異常安全保證)。

示例

#include <iomanip>
#include <iostream>
#include <string>

void print(std::string::size_type n,
           std::string::size_type len,
           std::string const &s)
{
    if (n == std::string::npos)
        std::cout << "没有找到\n";
    else
        std::cout << "在位置 " << n << " 找到了 " << std::quoted(s.substr(n, len)) << '\n';
}

int main()
{
    std::string::size_type n;
    std::string const s = "This is a string";
    
    // 从字符串尾反向搜索
    n = s.rfind("is");
    print(n, 2, s);
    
    // 从位置 4 反向搜索
    n = s.rfind("is", 4);
    print(n, 2, s);
    
    // 寻找单个字符
    n = s.rfind('s');
    print(n, 1, s);
    
    // 寻找单个字符
    n = s.rfind('q');
    print(n, 1, s);
    
    // 寻找前缀(参见 s.starts_with("This"))
    n = s.rfind("This", 0);
    print(n, 4, s);
}

輸出:

在位置 5 找到了 "is"
在位置 2 找到了 "is"
在位置 10 找到了 "s"
没有找到
在位置 0 找到了 "This"

缺陷報告

下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。

缺陷報告 應用於 出版時的行為 正確行為
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 丟棄 恢復

參閱

尋找給定子串的首次出現
(公開成員函數) [編輯]
尋找字符的首次出現
(公開成員函數) [編輯]
尋找字符的首次缺失
(公開成員函數) [編輯]
尋找字符的最後一次出現
(公開成員函數) [編輯]
尋找字符的最後一次缺失
(公開成員函數) [編輯]
尋找子串的最後一次出現
(std::basic_string_view<CharT,Traits> 的公開成員函數) [編輯]