Namespaces
Variants

std::basic_string_view<CharT,Traits>::find_last_of

来自 zh.cppreference.net
 
 
 
 
constexpr size_type
    find_last_of( basic_string_view v, size_type pos = npos ) const noexcept;
(1) (C++17 起)
constexpr size_type
    find_last_of( CharT ch, size_type pos = npos ) const noexcept;
(2) (C++17 起)
constexpr size_type
    find_last_of( const CharT* s, size_type pos, size_type count ) const;
(3) (C++17 起)
constexpr size_type
    find_last_of( const CharT* s, size_type pos = npos ) const;
(4) (C++17 起)

查找最后一个与给定字符序列中任一字符相等的字符。不指定精确搜索算法。搜索仅考虑区间 [0, pos]。若区间内不存在该字符,则返回 npos。

1) 在此视图中查找最后一个属于 v 中任意字符的字符,搜索结束于位置 pos。
2) 等价于 find_last_of(basic_string_view(std::addressof(ch), 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>
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 /* 位置 [0..2]: "els" */) &&
      //  └────────────────────────┘
    N == "explicit"sv.find_last_of("abcd"sv, 4 /* 位置 [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 /* 位置 [0..2]: "inl" */) &&
      //  └───────────────────────┘
    N == "mutable"sv.find_last_of('a', 2 /* 位置 [0..2]: "mut" */) &&
      //
    3 == "namespace"sv.find_last_of("cdef", 3 /* 位置 [0..3]: "name" */, 3 /* "cde" */) &&
      //     └─────────────────────────┘
    N == "namespace"sv.find_last_of("cdef", 3 /* 位置 [0..3]: "name" */, 2 /* "cd" */)
);
int main() {}

参见

在视图中查找字符
(公开成员函数)
查找子串的最后一次出现
(公开成员函数)
查找字符的首次出现
(公开成员函数)
查找第一个非指定字符的位置
(公开成员函数)
查找最后一个非指定字符的位置
(公开成员函数)
查找字符的最后一次出现
(std::basic_string<CharT,Traits,Allocator>的公开成员函数)