cpp/string/basic string view/find first of:修订间差异

来自cppreference.com
Lynnboy留言 | 贡献
无编辑摘要
Lynnboy留言 | 贡献
无编辑摘要
 
第3行: 第3行:
{{dcl begin}}
{{dcl begin}}
{{dcl|num=1|since=c++17|1=
{{dcl|num=1|since=c++17|1=
constexpr size_type find_first_of( basic_string_view v, size_type pos = 0 ) const noexcept;
constexpr size_type
find_first_of( basic_string_view v, size_type pos = 0 ) const noexcept;
}}
}}
{{dcl|num=2|since=c++17|1=
{{dcl|num=2|since=c++17|1=
constexpr size_type find_first_of( CharT ch, size_type pos = 0 ) const noexcept;
constexpr size_type
find_first_of( CharT ch, size_type pos = 0 ) const noexcept;
}}
}}
{{dcl|num=3|since=c++17|
{{dcl|num=3|since=c++17|
constexpr size_type find_first_of( const CharT* s, size_type pos, size_type count ) const;
constexpr size_type
find_first_of( const CharT* s, size_type pos, size_type count ) const;
}}
}}
{{dcl|num=4|since=c++17|1=
{{dcl|num=4|since=c++17|1=
constexpr size_type find_first_of( const CharT* s, size_type pos = 0 ) const;
constexpr size_type
find_first_of( const CharT* s, size_type pos = 0 ) const;
}}
}}
{{dcl end}}
{{dcl end}}
第39行: 第43行:


===示例===
===示例===
{{example||code=
{{example
#include <iostream>
|code=
#include <string_view>
#include <string_view>


int main()
 
()
{
{
     using namespace std::literals;
     ;
    constexpr auto N = std::string_view::npos;
;


     auto is_white_space = [](const char c) noexcept
     {
        return " \t\n\f\r\v"sv.find_first_of(c) != N;
     };
     = (c)
    
""sv.find_first_of()
=
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')
    );
    std::cout << "通过所有测试。\n";
}
|output=通过所有测试。
}}
}}



2024年4月30日 (二) 13:12的最新版本

 
 
 
 
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) 寻找 v 的任意字符在此视图中的首次出现,从位置 pos 开始。
2) 等价于 find_first_of(basic_string_view(std::addressof(c), 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 - 指向要搜索的字符串的指针
c - 要搜索的字符

返回值

子串的任意字符的首次出现位置,或者若找不到这些字符则为 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> 的公开成员函数) [编辑]