std::string_view find_last_of() 方法
- 自 C++17 起
// (1) Const version only
constexpr size_type find_last_of( const basic_string& str, size_type pos = 0 ) const noexcept;
// (2) Const version only
constexpr size_type find_last_of( const CharT* s, size_type pos, size_type count ) const;
// (3) Const version only
constexpr size_type find_last_of( const CharT* s, size_type pos = 0 ) const;
// (4) Const version only
constexpr size_type find_last_of( CharT ch, size_type pos = 0 ) const noexcept;
查找与给定字符序列中某个字符相同的最后一个字符。搜索仅考虑 **[ 0; pos ]** 区间。
- (1) 在此视图中查找 v 中任何字符的最后一次出现,终止于位置 pos。
- (2) 等同于
find_last_of(basic_string_view(std::addressof(c), 1), pos)
。 - (3) 等同于
find_last_of(basic_string_view(s, count), pos)
。 - (4) 等同于
find_last_of(basic_string_view(s), pos)
。
参数
v
- 要搜索的视图pos
- 搜索结束的位置count
- 要搜索的字符字符串的长度s
- 指向要搜索的字符字符串的指针ch
- 要搜索的字符
返回值
子字符串中任何字符的最后一次出现的位置,如果未找到此类子字符串,则为 npos
。
复杂度
最坏情况下为 O(size() * v.size())。
重要
本节需要改进。您可以通过编辑此文档页面来帮助我们。
异常
(无)
示例
#include <string_view>
#include <iostream>
int main()
{
using namespace std::literals;
constexpr auto N = std::string_view::npos;
static_assert(
5 == "delete"sv.find_last_of("cdef"sv) &&
// └────────────────────┘
N == "double"sv.find_last_of("fghi"sv) &&
//
0 == "else"sv.find_last_of("bcde"sv, 2 /* pos [0..2]: "els" */) &&
// └────────────────────────┘
N == "explicit"sv.find_last_of("abcd"sv, 4 /* pos [0..4]: "expli" */) &&
//
3 == "extern"sv.find_last_of('e') &&
// └────────────────────┘
N == "false"sv.find_last_of('x') &&
//
0 == "inline"sv.find_last_of('i', 2 /* pos [0..2]: "inl" */) &&
// └───────────────────────┘
N == "mutable"sv.find_last_of('a', 2 /* pos [0..2]: "mut" */) &&
//
3 == "namespace"sv.find_last_of("cdef", 3 /* pos [0..3]: "name" */, 3 /* "cde" */) &&
// └─────────────────────────┘
N == "namespace"sv.find_last_of("cdef", 3 /* pos [0..3]: "name" */, 2 /* "cd" */)
);
std::cout << "All tests passed.\n";
}
输出
All tests passed.