std::basic_string<CharT,Traits,Allocator>::rfind
来自 cppreference.cn
< cpp | string | basic string
size_type rfind( const basic_string& str, size_type pos = npos ) const; |
(1) | (noexcept since C++11) (constexpr since C++20) |
size_type rfind( const CharT* s, size_type pos, size_type count ) const; |
(2) | (constexpr since C++20) |
size_type rfind( const CharT* s, size_type pos = npos ) const; |
(3) | (constexpr since C++20) |
size_type rfind( CharT ch, size_type pos = npos ) const; |
(4) | (noexcept since C++11) (constexpr since C++20) |
template< class StringViewLike > size_type rfind( const StringViewLike& t, |
(5) | (since C++17) (constexpr since C++20) |
查找等于给定字符序列的最后一个子字符串。搜索从 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 的最后一个字符。
仅当 std::is_convertible_v<const StringViewLike&,
std::basic_string_view<CharT, Traits>> 为 true 且 std::is_convertible_v<const StringViewLike&, const CharT*> 为 false 时,此重载才参与重载决议。
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 的情况。
[编辑] 异常
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++ 标准。
DR | 应用于 | 已发布行为 | 正确行为 |
---|---|---|---|
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 意外删除 |
已恢复 |
[编辑] 参见
查找给定子字符串的首次出现 (public member function) | |
查找字符的首次出现 (public member function) | |
查找字符的首次缺失 (public member function) | |
查找字符的最后一次出现 (public member function) | |
查找字符的最后一次缺失 (public member function) | |
查找子字符串的最后一次出现 ( std::basic_string_view<CharT,Traits> 的公共成员函数) |