std::string rfind() 方法
- 自 C++20 起
- 自 C++17 起
- 自 C++11 起
- 直到 C++11
// (1) Const version only
constexpr size_type rfind( const basic_string& str, size_type pos = 0 ) const noexcept;
// (2) Const version only
constexpr size_type rfind( const CharT* s, size_type pos, size_type count ) const;
// (3) Const version only
constexpr size_type rfind( const CharT* s, size_type pos = 0 ) const;
// (4) Const version only
constexpr size_type rfind( CharT ch, size_type pos = 0 ) const noexcept;
// (5) Const version only
template < class StringViewLike >
constexpr size_type rfind( const StringViewLike& t, size_type pos = 0 ) const noexcept(/* see below */);
// (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 */);
// (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;
// (1) Const version only
size_type rfind( const basic_string& str, size_type pos = 0 ) const;
// (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;
查找最后一个等于给定字符序列的子字符串。
搜索从 pos
开始,即找到的子字符串不能开始于 pos
之后的位置。
-
(1) 查找最后一个等于
str
的子字符串。 -
(2) 查找最后一个等于范围 [ s, s + count ) 的子字符串。
此范围可以包含空字符。 -
(3) 查找最后一个等于
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
时参与重载决议。
如果 npos
或任何不小于 size() - 1
的值作为 pos 传递,则将搜索整个字符串。
参数
str
- 要搜索的字符串pos
- 开始搜索的位置count
- 要搜索的子字符串的长度s
- 指向要搜索的字符字符串的指针ch
- 要搜索的字符t
- 要搜索的对象(可转换为std::basic_string_view
)
返回值
找到子字符串的第一个字符的位置,如果未找到此类子字符串,则为 npos
。
这是从字符串开头计算的偏移量,而不是从结尾计算。
如果搜索空字符串(str.size()
、count
或 Traits::length(s)
为零),则返回 pos
(空字符串立即找到),除非 pos > size()
(包括 pos == npos
的情况),在这种情况下返回 size()
。
复杂度
本节需要改进。您可以通过编辑此文档页面来帮助我们。
异常
- (1-4) (无)
- (5) noexcept 规范
noexcept(std::is_nothrow_convertible_v<const T&, std::basic_string_view<CharT, Traits>>)
备注
Traits::eq()
用于比较。默认情况下,`Traits` 是 std::char_traits<CharT>
。
示例
#include <string>
#include <iostream>
void print(std::string::size_type n, std::string const &s)
{
if (n == std::string::npos) {
std::cout << "not found\n";
} else {
std::cout << "found: \"" << s.substr(n) << "\" 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, s);
// search backwards from position 4
n = s.rfind("is", 4);
print(n, s);
// find a single character
n = s.rfind('s');
print(n, s);
// find a single character
n = s.rfind('q');
print(n, s);
}
found: "is a string" at 5
found: "is is a string" at 2
found: "string" at 10
not found