std::basic_string<CharT,Traits,Allocator>::find_last_not_of
来自cppreference.com
                    
                                        
                    < cpp | string | basic string
                    
                                                            
                    | (1) | ||
| size_type find_last_not_of( const basic_string& str, size_type pos = npos ) const; | (C++11 前) | |
| size_type find_last_not_of( const basic_string& str, size_type pos = npos ) const noexcept; | (C++11 起) (C++20 前) | |
| constexpr size_type find_last_not_of( const basic_string& str, size_type pos = npos ) const noexcept; | (C++20 起) | |
| (2) | ||
| size_type find_last_not_of( const CharT* s, size_type pos, size_type count ) const; | (C++20 前) | |
| constexpr size_type find_last_not_of( const CharT* s, size_type pos, size_type count ) const; | (C++20 起) | |
| (3) | ||
| size_type find_last_not_of( const CharT* s, size_type pos = npos ) const; | (C++20 前) | |
| constexpr size_type find_last_not_of( const CharT* s, size_type pos = npos ) const; | (C++20 起) | |
| (4) | ||
| size_type find_last_not_of( CharT ch, size_type pos = npos ) const; | (C++11 前) | |
| size_type find_last_not_of( CharT ch, size_type pos = npos ) const noexcept; | (C++11 起) (C++20 前) | |
| constexpr size_type find_last_not_of( 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 起) | |
寻找不等于给定字符序列中任何字符的最后字符。搜索只考虑区间 [0, pos]。如果区间中不存在这种字符,那么返回 npos。
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 | - | 鉴别要搜索的字符的字符串 | 
| pos | - | 搜索结束的位置 | 
| count | - | 鉴别要搜索的字符的字符串长度 | 
| s | - | 指向鉴别要搜索的字符的字符串的指针 | 
| ch | - | 鉴别要搜索的字符的字符 | 
| t | - | 鉴别要搜索的字符的对象(可转换为 std::basic_string_view) | 
返回值
找到的字符位置,在找不到这种字符时返回 npos。
异常
5) 
noexcept 说明:  
noexcept(std::is_nothrow_convertible_v<const T&, std::basic_string_view<charT, traits>>)
示例
运行此代码
#include <iostream> #include <string> void show_pos(const std::string& str, std::string::size_type found) { if (found != std::string::npos) std::cout << "[" << found << "] = \'" << str[found] << "\'\n"; else std::cout << "没有找到" "\n"; } int main() { std::string str{"abc_123"}; char const* skip_set{"0123456789"}; std::string::size_type str_last_pos{std::string::npos}; show_pos(str, str.find_last_not_of(skip_set)); // [3] = '_' str_last_pos = 2; show_pos(str, str.find_last_not_of(skip_set, str_last_pos)); // [2] = 'c' str_last_pos = 2; show_pos(str, str.find_last_not_of('c', str_last_pos)); // [1] = 'b' const char arr[]{'3', '4', '5'}; show_pos(str, str.find_last_not_of(arr)); // [5] = '2' str_last_pos = 2; std::string::size_type skip_set_size{4}; show_pos(str, str.find_last_not_of(skip_set, str_last_pos, skip_set_size)); // [2] = 'c' show_pos(str, str.find_last_not_of("abc")); // [6] = '3' str_last_pos = 2; show_pos(str, str.find_last_not_of("abc", str_last_pos)); // 没有找到 }
输出:
[3] = '_' [2] = 'c' [1] = 'b' [5] = '2' [2] = 'c' [6] = '3' 没有找到
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| 缺陷报告 | 应用于 | 出版时的行为 | 正确行为 | 
|---|---|---|---|
| LWG 141 | C++98 | 重载 (1) 在 pos >= size() 时只能返回 npos | 此时搜索范围是 [0, size()) | 
| LWG 2064 | C++11 | 重载 (3) 与 (4) 是 noexcept 的 | 移除 | 
| LWG 2946 | C++17 | string_view重载在某些情况下会导致歧义 | 通过改成模板来避免 | 
| P1148R0 | C++11 C++17 | 重载 (4)/(5) 的 noexcept 意外地被 LWG 问题 2064/LWG 问题 2946 的解决方案丢弃 | 恢复 | 
参阅
| 于字符串中寻找字符 (公开成员函数) | |
| 寻找子串的最后一次出现 (公开成员函数) | |
| 寻找字符的首次出现 (公开成员函数) | |
| 寻找字符的首次缺失 (公开成员函数) | |
| 寻找字符的最后一次出现 (公开成员函数) |