Namespaces
Variants

std::basic_string<CharT,Traits,Allocator>::find_last_not_of

来自 zh.cppreference.net
 
 
 
std::basic_string
 
size_type find_last_not_of( const basic_string& str,
                            size_type pos = npos ) const;
(1) (C++11 起为 noexcept)
(C++20 起为 constexpr)
size_type find_last_not_of( const CharT* s,
                            size_type pos, size_type count ) const;
(2) (C++20 起为 constexpr)
size_type find_last_not_of( const CharT* s, size_type pos = npos ) const;
(3) (C++20 起为 constexpr)
size_type find_last_not_of( CharT ch, size_type pos = npos ) const;
(4) (C++11 起为 noexcept)
(C++20 起为 constexpr)
template< class StringViewLike >
size_type
    find_last_not_of( const StringViewLike& t,
                      size_type pos = npos ) const noexcept(/* see below */);
(5) (C++17 起)
(C++20 起为 constexpr)

寻找第一个与给定字符序列中任一字符都不相等的最后一个字符。搜索仅考虑范围 [0, pos]。如果范围中的所有字符都能在给定字符序列中找到,则返回 npos。

1) 寻找与 str 中任一字符都不相等的最后一个字符。
2) 寻找与范围 [s, s + count) 中任一字符都不相等的最后一个字符。此范围可以包含空字符。
如果 [s, s + count) 不是 有效范围,则行为未定义。
3) 寻找与指向 s 的字符串中任一字符都不相等的最后一个字符。字符串长度由首个空字符决定,使用 Traits::length(s)。
如果 [s, 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 时才会参与重载决议。

在所有情况下,相等性通过调用 Traits::eq 来检查。

参数

str - 用于标识要搜索字符的字符串
pos - 结束搜索的位置
count - 标识要搜索字符的字符串长度
s - 指向标识要搜索字符的字符串的指针
ch - 标识要搜索字符的字符
t - 可转换为 std::basic_string_view 的对象,用于标识要搜索的字符

返回值

找到字符的位置,若未找到此类字符则返回 npos 。

异常

1,4) 不抛出任何异常。
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 << "not found\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)); // not found
}

输出:

[3] = '_'
[2] = 'c'
[1] = 'b'
[5] = '2'
[2] = 'c'
[6] = '3'
not found

缺陷报告

以下行为变更缺陷报告被追溯应用于先前发布的C++标准。

缺陷报告 应用于 发布时行为 正确行为
LWG 141 C++98 重载 (1) 仅在 npos 时可能返回 pos >= size ( ) 此时搜索范围为
[ ​ 0 ​ , size ( ) )
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 意外丢弃
已恢复

参见

查找给定子串的首次出现
(公开成员函数)
查找子串的最后一次出现
(公开成员函数)
查找字符的首次出现
(公开成员函数)
查找字符的首次缺失
(公开成员函数)
查找字符的最后一次出现
(公开成员函数)
查找字符的最后一次缺失
( std::basic_string_view<CharT,Traits> 的公开成员函数)