Namespaces
Variants

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

From cppreference.net
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 ( /* see below */ ) ;
(5) (自 C++17 起)
(自 C++20 起 constexpr)

查找与给定字符序列相等的最后一个子串。搜索从 pos 开始并从右向左进行(因此找到的子串(如果存在)其起始位置不能超过 pos )。如果传入 npos 或任何不小于 size() - 1 的值作为 pos ,将会搜索整个字符串。

1) 查找最后一个等于 str 的子字符串。
2) 查找与范围 [ s , s + count ) 相等的最后一个子串。此范围可以包含空字符。
如果 [ s , s + count ) 不是有效的 范围 ,则行为未定义。
3) 查找与 s 所指向字符串相等的最后一个子串。字符串长度由首个空字符通过 Traits :: length ( s ) 确定。
如果 [ s , s + 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 >>
true std:: is_convertible_v < const StringViewLike & , const CharT * > false 时参与重载决议。

在所有情况下,通过调用 Traits::eq 来检查相等性。

目录

参数

str - 要搜索的字符串
pos - 开始搜索的位置
count - 要搜索的子串长度
s - 要搜索的字符字符串指针
ch - 要搜索的字符
t - 要搜索的对象(可转换为 std::basic_string_view

返回值

找到的子串首字符位置,若未找到该子串则为 npos 。请注意这是从字符串起始处计算的偏移量,而非从末尾计算。

若搜索空字符串(即 str. size ( ) count Traits :: 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";
    // 从字符串末尾向前搜索
    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);
}

输出:

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> 的公开成员函数)