std::string_view rfind() 方法
- 自 C++17 起
// (1) Const version only
size_type rfind( const basic_string& str, size_type pos = 0 ) const noexcept;
// (2) Const version only
size_type rfind( const CharT* s, size_type pos, size_type count ) const;
// (3) Const version only
size_type rfind( const CharT* s, size_type pos = 0 ) const;
// (4) Const version only
size_type rfind( CharT ch, size_type pos = 0 ) const noexcept;
// (5) Const version only
template < class StringViewLike >
size_type rfind( const StringViewLike& t, size_type pos = 0 ) const noexcept(/* see below */);
查找与给定字符序列相等的最后一个子字符串。
搜索从 pos
开始,即找到的子字符串不能以 pos
之后的任何位置开始。
如果将 npos
或任何不小于 size() - 1
的值作为 pos
传递,则将搜索整个字符串。
- (1) 从位置
pos
开始,在此视图中查找v
的最后一次出现。 - (2) 等同于
rfind(basic_string_view(std::addressof(c), 1), pos)
。 - (3) 等同于
rfind(basic_string_view(s, count), pos)
。 - (4) 等同于
rfind(basic_string_view(s), pos)
。
参数
v
- 要搜索的视图pos
- 开始搜索的位置count
- 要搜索的子字符串的长度s
- 指向要搜索的字符字符串的指针ch
- 要搜索的字符
返回值
找到的子字符串的第一个字符的位置,如果未找到此类子字符串,则为 npos
。
复杂度
最坏情况下为 O(size() * v.size())。
重要
本节需要改进。您可以通过编辑此文档页面来帮助我们。
异常
(无)
示例
#include <string_view>
int main()
{
using namespace std::literals;
constexpr auto N = std::string_view::npos;
static_assert(true
&& (6 == "AB AB AB"sv.rfind("AB"))
&& (6 == "AB AB AB"sv.rfind("ABCD", N, 2))
&& (3 == "AB AB AB"sv.rfind("AB", 5))
&& (2 == "B AB AB "sv.rfind("AB", 2))
&& (N == "B AB AB "sv.rfind("AB", 1))
&& (5 == "B AB AB "sv.rfind('A'))
&& (4 == "AB AB AB"sv.rfind('B', 4))
&& (N == "AB AB AB"sv.rfind('C'))
);
}