名稱空間
變體
操作

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

來自 cppreference.com
 
 
 
 
constexpr size_type
    find_first_of( basic_string_view v, size_type pos = 0 ) const noexcept;
(1) (C++17 起)
constexpr size_type
    find_first_of( CharT ch, size_type pos = 0 ) const noexcept;
(2) (C++17 起)
constexpr size_type
    find_first_of( const CharT* s, size_type pos, size_type count ) const;
(3) (C++17 起)
constexpr size_type
    find_first_of( const CharT* s, size_type pos = 0 ) const;
(4) (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> 的公共成員函式) [編輯]