public member function
<string>

std::basic_string::rfind

string (1)
size_type rfind (const basic_string& str, size_type pos = npos) const;
c-string (2)
size_type rfind (const charT* s, size_type pos = npos) const;
buffer (3)
size_type rfind (const charT* s, size_type pos, size_type n) const;
character (4)
size_type rfind (charT c, size_type pos = npos) const;
string (1)
size_type rfind (const basic_string& str, size_type pos = npos) const noexcept;
c-string (2)
size_type rfind (const charT* s, size_type pos = npos) const;
buffer (3)
size_type rfind (const charT* s, size_type pos, size_type n) const;
character (4)
size_type rfind (charT c, size_type pos = npos) const noexcept;
查詢字串中最後一次出現
basic_string中搜索其引數指定的序列的最後一次出現。

當指定了pos時,搜尋只包括從位置pos或之前開始的字元序列,忽略在pos之後開始的任何可能的匹配。

該函式使用traits_type::eq來確定字元的等價性。

引數

str
另一個要搜尋的basic_string
pos
要被認為是匹配開始的字串中最後一個字元的位置。
任何大於或等於字串長度的值(包括basic_string::npos)都意味著搜尋整個字串。
注意:第一個字元用值表示0(不是1).
s
指向字元陣列的指標。
如果指定了引數n (3),則要匹配的序列是陣列中的前n個字元。
否則(2),期望一個以 null 結尾的序列:要匹配的序列的長度由第一個 null 字元確定。
n
要匹配的字元序列的長度。
c
要搜尋的單個字元。

charTbasic_string 的字元型別(即,它的第一個模板引數)。
成員型別size_type是一種無符號整型型別。

返回值

最後一次匹配的第一個字元的位置。
如果沒有找到匹配項,該函式返回basic_string::npos

成員型別size_type是一種無符號整型型別。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// string::rfind
#include <iostream>
#include <string>

int main ()
{
  std::string str ("The sixth sick sheik's sixth sheep's sick.");
  std::string key ("sixth");

  std::string::size_type found = str.rfind(key);
  if (found!=std::string::npos)
    str.replace (found,key.length(),"seventh");

  std::cout << str << '\n';

  return 0;
}

The sixth sick sheik's seventh sheep's sick.


複雜度

未指定,但通常最多是字串長度(或pos)乘以要匹配的字元數(最壞情況)的線性關係。

迭代器有效性

沒有變化。

資料競爭

該物件被訪問。

異常安全

如果已知s未指向足夠長的陣列,會導致未定義行為
否則,該函式永遠不會丟擲異常(無丟擲保證)。

另見