std::string find_last_not_of() 方法
- 自 C++20 起
- 自 C++17 起
- 自 C++11 起
- 直到 C++11
// (1) Const version only
constexpr size_type find_last_not_of( const basic_string& str, size_type pos = 0 ) const noexcept;
// (2) Const version only
constexpr size_type find_last_not_of( const CharT* s, size_type pos, size_type count ) const;
// (3) Const version only
constexpr size_type find_last_not_of( const CharT* s, size_type pos = 0 ) const;
// (4) Const version only
constexpr size_type find_last_not_of( CharT ch, size_type pos = 0 ) const noexcept;
// (5) Const version only
template < class StringViewLike >
constexpr size_type find_last_not_of( const StringViewLike& t, size_type pos = 0 ) const noexcept(/* see below */);
// (1) Const version only
size_type find_last_not_of( const basic_string& str, size_type pos = 0 ) const noexcept;
// (2) Const version only
size_type find_last_not_of( const CharT* s, size_type pos, size_type count ) const;
// (3) Const version only
size_type find_last_not_of( const CharT* s, size_type pos = 0 ) const;
// (4) Const version only
size_type find_last_not_of( CharT ch, size_type pos = 0 ) const noexcept;
// (5) Const version only
template < class StringViewLike >
size_type find_last_not_of( const StringViewLike& t, size_type pos = 0 ) const noexcept(/* see below */);
// (1) Const version only
size_type find_last_not_of( const basic_string& str, size_type pos = 0 ) const noexcept;
// (2) Const version only
size_type find_last_not_of( const CharT* s, size_type pos, size_type count ) const;
// (3) Const version only
size_type find_last_not_of( const CharT* s, size_type pos = 0 ) const;
// (4) Const version only
size_type find_last_not_of( CharT ch, size_type pos = 0 ) const noexcept;
// (1) Const version only
size_type find_last_not_of( const basic_string& str, size_type pos = 0 ) const;
// (2) Const version only
size_type find_last_not_of( const CharT* s, size_type pos, size_type count ) const;
// (3) Const version only
size_type find_last_not_of( const CharT* s, size_type pos = 0 ) const;
// (4) Const version only
size_type find_last_not_of( CharT ch, size_type pos = 0 ) const;
查找与给定字符序列中“**所有字符都不相同**”的最后一个字符。
搜索仅考虑区间 **[ 0, 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` 时参与重载决议。
参数
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>>)
备注
Traits::eq()
用于比较。默认情况下,`Traits` 是 std::char_traits<CharT>
。
示例
#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