C++ 参考手册
- C++11
- C++14
- C++17
- C++20
- C++ 编译器支持情况表
- 独立与宿主实现
- C++ 语言
- C++ 关键词
- 预处理器
- C++ 标准库头文件
- 具名要求
- 功能特性测试 (C++20)
- 工具库
- 类型支持(基本类型、RTTI、类型特性)
- 概念库 (C++20)
- 错误处理
- 动态内存管理
- 日期和时间工具
- 字符串库
- std::basic_string
- std::basic_string<CharT,Traits,Allocator>::npos
- std::basic_string<CharT,Traits,Allocator>::at
- std::hash (std::string, std::wstring, std::u16string, std::u32string, std::pmr::string, std::pmr::wstring, std::pmr::u16string, std::pmr::u32string)
- std::basic_string<CharT,Traits,Allocator>::assign
- std::basic_string<CharT,Traits,Allocator>::get_allocator
- std::basic_string<CharT,Traits,Allocator>::substr
- std::basic_string<CharT,Traits,Allocator>::basic_string
- std::basic_string<CharT,Traits,Allocator>::operator=
- std::basic_string<CharT,Traits,Allocator>::operator[]
- std::basic_string<CharT,Traits,Allocator>::front
- std::basic_string<CharT,Traits,Allocator>::back
- std::basic_string<CharT,Traits,Allocator>::data
- std::basic_string<CharT,Traits,Allocator>::c_str
- std::basic_string<CharT,Traits,Allocator>::operator basic_string_view
- std::basic_string<CharT,Traits,Allocator>::begin, std::basic_string<CharT,Traits,Allocator>::cbegin
- std::basic_string<CharT,Traits,Allocator>::end, std::basic_string<CharT,Traits,Allocator>::cend
- std::basic_string<CharT,Traits,Allocator>::rbegin, std::basic_string<CharT,Traits,Allocator>::crbegin
- std::basic_string<CharT,Traits,Allocator>::rend, std::basic_string<CharT,Traits,Allocator>::crend
- std::basic_string<CharT,Traits,Allocator>::empty
- std::basic_string<CharT,Traits,Allocator>::size, std::basic_string<CharT,Traits,Allocator>::length
- std::basic_string<CharT,Traits,Allocator>::max_size
- std::basic_string<CharT,Traits,Allocator>::reserve
- std::basic_string<CharT,Traits,Allocator>::capacity
- std::basic_string<CharT,Traits,Allocator>::shrink_to_fit
- std::basic_string<CharT,Traits,Allocator>::clear
- std::basic_string<CharT,Traits,Allocator>::insert
- std::basic_string<CharT,Traits,Allocator>::erase
- std::basic_string<CharT,Traits,Allocator>::push_back
- std::basic_string<CharT,Traits,Allocator>::pop_back
- std::basic_string<CharT,Traits,Allocator>::append
- std::basic_string<CharT,Traits,Allocator>::operator+=
- std::basic_string<CharT,Traits,Allocator>::compare
- std::basic_string<CharT,Traits,Allocator>::starts_with
- std::basic_string<CharT,Traits,Allocator>::ends_with
- std::basic_string<CharT,Traits,Allocator>::replace
- std::basic_string<CharT,Traits,Allocator>::copy
- std::basic_string<CharT,Traits,Allocator>::resize
- std::basic_string<CharT,Traits,Allocator>::swap
- std::basic_string<CharT,Traits,Allocator>::find
- std::basic_string<CharT,Traits,Allocator>::rfind
- std::basic_string<CharT,Traits,Allocator>::find_first_of
- std::basic_string<CharT,Traits,Allocator>::find_first_not_of
- std::basic_string<CharT,Traits,Allocator>::find_last_of
- std::basic_string<CharT,Traits,Allocator>::find_last_not_of
- std::operator+(std::basic_string)
- std::swap(std::basic_string)
- std::literals::string_literals::operator""s
- std::erase, std::erase_if (std::basic_string)
- operator<<,>>(std::basic_string)
- std::getline
- operator==,!=,<,<=,>,>=,<=>(std::basic_string)
- std::stoi, std::stol, std::stoll
- std::stoul, std::stoull
- std::stof, std::stod, std::stold
- std::to_string
- std::to_wstring
- std::basic_string 的推导指引
- std::basic_string_view
- 空终止字节字符串
- 空终止多字节字符串
- 空终止宽字符串
- std::char_traits
- 注释
- 容器库
- 迭代器库
- 范围库 (C++20)
- 算法库
- 数值库
- 输入/输出库
- 文件系统库
- 本地化库
- 正则表达式库
- 原子操作库
- 线程支持库
- 实验性 C++ 特性
- 有用的资源
- 索引
- std 符号索引
- 协程支持 (C++20)
- C++ 关键词
位置:首页 > C++ 参考手册 >字符串库 >std::basic_string > std::basic_string<CharT,Traits,Allocator>::rfind
std::basic_string<CharT,Traits,Allocator>::rfind
(1) | ||
size_type rfind( const basic_string& str, size_type pos = npos ) const; |
(C++11 前) | |
size_type rfind( const basic_string& str, size_type pos = npos ) const noexcept; |
(C++11 起) (C++20 前) |
|
constexpr size_type rfind( const basic_string& str, size_type pos = npos ) const noexcept; |
(C++20 起) | |
(2) | ||
size_type rfind( const CharT* s, size_type pos, size_type count ) const; |
(C++20 前) | |
constexpr size_type rfind( const CharT* s, size_type pos, size_type count ) const; |
(C++20 起) | |
(3) | ||
size_type rfind( const CharT* s, size_type pos = npos ) const; |
(C++20 前) | |
constexpr size_type rfind( const CharT* s, size_type pos = npos ) const; |
(C++20 起) | |
(4) | ||
size_type rfind( CharT ch, size_type pos = npos ) const; |
(C++11 前) | |
size_type rfind( CharT ch, size_type pos = npos ) const noexcept; |
(C++11 起) (C++20 前) |
|
constexpr size_type rfind( CharT ch, size_type pos = npos ) const noexcept; |
(C++20 起) | |
(5) | ||
template < class T > size_type |
(C++17 起) (C++20 前) |
|
template < class T > constexpr size_type |
(C++20 起) | |
寻找等于给定字符序列的最后子串。搜索始于 pos
,即找到的子串必须不始于 pos
后的位置。若将 npos 或任何不小于 size()-1 的值作为 pos
传递,则在整个字符串中搜索。
1) 寻找等于
str
的最后子串。2) 寻找等于范围
[s, s+count)
的最后子串。此范围能包含空字符。3) 寻找等于
s
所指向的字符串的最后子串。由首个空字符,用 Traits::length(s)
确定字符串长度。4) 寻找等于
ch
的最后字符。5) 如同用 std::basic_string_view<CharT, Traits> sv = t; 隐式转换
t
为 string_view sv
,然后寻找等于 sv
内容的最后子串。此重载仅若 std::is_convertible_v<const T&, std::basic_string_view<CharT, Traits>> 为 true 且 std::is_convertible_v<const T&, const CharT*> 为 false 才参与重载决议。所有情况下均调用 Traits::eq 检查相等性。
参数
str | - | 要搜索的 string |
pos | - | 开始搜索的位置 |
count | - | 要搜索的子串长度 |
s | - | 指向要搜索的字符串的指针 |
ch | - | 要搜索的字符 |
t | - | 要搜索的对象(可转换为 std::basic_string_view ) |
返回值
找到的子串的首字符位置,或若找不到这种子串则为 npos 。注意这是从字符串开始,而非末尾的偏移。
若搜索任何空字符串( str.size() 、 count 或 Traits::length(s) 为零),则返回 pos
(立即找到空字符串),除非 pos > size() (包括 pos == npos 的情况),该情况下返回 size() 。
异常
5)
noexcept 规定:
noexcept(std::is_nothrow_convertible_v<const T&, std::basic_string_view<CharT, Traits>>)
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
DR | 应用于 | 出版时的行为 | 正确行为 |
---|---|---|---|
LWG 2946 | C++17 | string_view 重载在某些情况下导致歧义
|
通过使之为模板来避免 |
示例
运行此代码
#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"; // 从字符串尾反向搜索 n = s.rfind("is"); print(n, s); // 从位置 4 反向搜索 n = s.rfind("is", 4); print(n, s); // 寻找单个字符 n = s.rfind('s'); print(n, s); // 寻找单个字符 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
参阅
于字符串中寻找字符 (公开成员函数) | |
寻找字符的首次出现 (公开成员函数) | |
寻找字符的首次缺失 (公开成员函数) | |
寻找字符的最后一次出现 (公开成员函数) | |
寻找字符的最后一次缺失 (公开成员函数) |