跳到主要内容

std::string_view find_first_of() 方法

// (1) Const version only
constexpr size_type find_first_of( const basic_string& str, size_type pos = 0 ) const noexcept;

// (2) Const version only
constexpr size_type find_first_of( const CharT* s, size_type pos, size_type count ) const;

// (3) Const version only
constexpr size_type find_first_of( const CharT* s, size_type pos = 0 ) const;

// (4) Const version only
constexpr size_type find_first_of( CharT ch, size_type pos = 0 ) const noexcept;

查找与给定字符序列中任何字符相等的第一个字符。

  • (1) 在此视图中,从位置 pos 开始,查找 v 中任何字符的第一次出现。
  • (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>
#include <iostream>

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;
};

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 << "All tests passed.\n";
}
输出
All tests passed.
本文档源自 此 CppReference 页面。可能已为改进或编辑偏好进行了修改。点击“编辑此页面”查看本文档所做的所有更改。
悬停查看原始许可证。

std::string_view find_first_of() 方法

// (1) Const version only
constexpr size_type find_first_of( const basic_string& str, size_type pos = 0 ) const noexcept;

// (2) Const version only
constexpr size_type find_first_of( const CharT* s, size_type pos, size_type count ) const;

// (3) Const version only
constexpr size_type find_first_of( const CharT* s, size_type pos = 0 ) const;

// (4) Const version only
constexpr size_type find_first_of( CharT ch, size_type pos = 0 ) const noexcept;

查找与给定字符序列中任何字符相等的第一个字符。

  • (1) 在此视图中,从位置 pos 开始,查找 v 中任何字符的第一次出现。
  • (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>
#include <iostream>

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;
};

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 << "All tests passed.\n";
}
输出
All tests passed.
本文档源自 此 CppReference 页面。可能已为改进或编辑偏好进行了修改。点击“编辑此页面”查看本文档所做的所有更改。
悬停查看原始许可证。