名稱空間
變體
操作

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

來自 cppreference.com
< cpp‎ | string‎ | basic_string
 
 
 
std::basic_string
 
size_type rfind( const basic_string& str, size_type pos = npos ) const;
(1) (C++11 起無異常丟擲)
(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 起無異常丟擲)
(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) 隱式地將 t 轉換為字串檢視 sv,如同透過 std::basic_string_view<CharT, Traits> sv = t;,然後查詢最後一個等於 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) 為零),則立即找到空字串,並且 rfind 返回

  • 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 << "not found\n";
    else
        std::cout << "found: " << std::quoted(s.substr(n, len)) << " at " << n << '\n';
}
 
int main()
{
    std::string::size_type n;
    std::string const s = "This is a string";
 
    // search backwards from end of string
    n = s.rfind("is");
    print(n, 2, s);
 
    // search backwards from position 4
    n = s.rfind("is", 4);
    print(n, 2, s);
 
    // find a single character
    n = s.rfind('s');
    print(n, 1, s);
 
    // find a single character
    n = s.rfind('q');
    print(n, 1, s);
 
    // find the prefix (see also s.starts_with("This"))
    n = s.rfind("This", 0);
    print(n, 4, s);
}

輸出

found: "is" at 5
found: "is" at 2
found: "s" at 10
not found
found: "This" at 0

[編輯] 缺陷報告

下列更改行為的缺陷報告追溯地應用於以前出版的 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> 的公共成員函式) [編輯]