命名空间
变体
操作

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

来自 cppreference.cn
 
 
 
 
constexpr size_type
    find_first_of( basic_string_view v, size_type pos = 0 ) const noexcept;
(1) (since C++17)
constexpr size_type
    find_first_of( CharT ch, size_type pos = 0 ) const noexcept;
(2) (since C++17)
constexpr size_type
    find_first_of( const CharT* s, size_type pos, size_type count ) const;
(3) (since C++17)
constexpr size_type
    find_first_of( const CharT* s, size_type pos = 0 ) const;
(4) (since C++17)

查找等于给定字符序列中任何字符的首个字符。

1) 查找此视图中从位置 pos 开始的 v 中任何字符的首个出现。
2) 等效于 find_first_of(basic_string_view(std::addressof(ch), 1), pos)
3) 等效于 find_first_of(basic_string_view(s, count), pos)
4) 等效于 find_first_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;
 
constexpr bool is_white_space(const char c)
{
    return " \t\n\f\r\v"sv.find_first_of(c) != N;
};
 
static_assert(
    1 == "alignas"sv.find_first_of("klmn"sv) &&
      //   └─────────────────────────┘
    N == "alignof"sv.find_first_of("wxyz"sv) &&
      //
    3 == "concept"sv.find_first_of("bcde"sv, /* pos= */ 1) &&
      //     └───────────────────────┘
    N == "consteval"sv.find_first_of("oxyz"sv, /* pos= */ 2) &&
      //
    6 == "constexpr"sv.find_first_of('x') &&
      //        └─────────────────────┘
    N == "constinit"sv.find_first_of('x') &&
      //
    6 == "const_cast"sv.find_first_of('c', /* pos= */ 4) &&
      //        └──────────────────────┘
    N == "continue"sv.find_first_of('c', /* pos= */ 42) &&
      //
    5 == "co_await"sv.find_first_of("cba", /* pos= */ 4) &&
      //       └───────────────────────┘
    7 == "decltype"sv.find_first_of("def", /* pos= */ 2, /* count= */ 2) &&
      //         └────────────────────┘
    N == "decltype"sv.find_first_of("def", /* pos= */ 2, /* count= */ 1) &&
      //
    is_white_space(' ') && is_white_space('\r') && !is_white_space('\a')
);
 
int main() {}

[编辑] 参见

在视图中查找字符
(公共成员函数) [编辑]
查找子字符串的最后一次出现
(公共成员函数) [编辑]
查找字符的最后一次出现
(公共成员函数) [编辑]
查找首个不包含字符的位置
(公共成员函数) [编辑]
查找最后不包含字符的位置
(公共成员函数) [编辑]
查找字符的首个出现位置
(std::basic_string<CharT,Traits,Allocator> 的公共成员函数) [编辑]