std::basic_string<CharT,Traits,Allocator>::find_first_of
来自 zh.cppreference.net
size_type find_first_of( const basic_string& str, size_type pos = 0 ) const;
|
(1) | (C++11 起为 noexcept) (C++20 起为 constexpr) |
size_type find_first_of( const CharT* s,
size_type pos, size_type count ) const;
|
(2) | (C++20 起为 constexpr) |
size_type find_first_of( const CharT* s, size_type pos = 0 ) const;
|
(3) | (C++20 起为 constexpr) |
size_type find_first_of( CharT ch, size_type pos = 0 ) const;
|
(4) | (C++11 起为 noexcept) (C++20 起为 constexpr) |
template< class StringViewLike >
size_type
find_first_of( const StringViewLike& t,
size_type pos = 0 ) const noexcept(/* see below */);
|
(5) | (C++17 起) (C++20 起为 constexpr) |
查找与给定字符序列中任一字符相等的第一个字符。搜索只考虑范围 [pos, size())。如果给定字符序列中没有字符出现在该范围内,则返回 npos。
1) 查找与
str 中任一字符相等的第一个字符。2) 查找与范围
[s, s + count) 中任一字符相等的第一个字符。该范围可以包含空字符。 如果
[s, s + count) 不是有效范围,则行为未定义。3) 查找与
s 所指向的字符串中任一字符相等的第一个字符。字符串长度由首个空字符通过 Traits::length(s) 确定。 如果
[s, s + Traits::length(s)) 不是有效范围,则行为未定义。4) 查找与
ch 相等的第一个字符。5) 将
t 隐式转换为字符串视图 sv,如同通过 std::basic_string_view<CharT, Traits> sv = t; 进行,然后查找与 sv 中任一字符相等的第一个字符。 仅当
std::is_convertible_v<const StringViewLike&,std::basic_string_view<CharT, Traits>> 为 true 且 std::is_convertible_v<const StringViewLike&, const CharT*> 为 false 时,此重载才会参与重载决议。参数
| str | - | 用于标识要搜索字符的字符串 |
| pos | - | 开始搜索的位置 |
| count | - | 标识要搜索字符的字符串长度 |
| s | - | 指向标识要搜索字符的字符串的指针 |
| ch | - | 要搜索的字符 |
| t | - | 对象(可转换为 std::basic_string_view )用于标识要搜索的字符 |
返回值
找到字符的位置,若未找到此类字符则返回 npos 。
异常
1,4)
不抛出任何异常。
5)
noexcept
规范:
noexcept
(
std::
is_nothrow_convertible_v
<
const
T
&
,
std::
basic_string_view
<
CharT, Traits
>>
)
若因任何原因抛出异常,此函数不产生任何效果( 强异常安全保证 )。
注释
Traits :: eq ( ) 用于执行比较操作。
示例
运行此代码
#include <cassert> #include <iostream> #include <string> #include <string_view> int main() { using namespace std::literals; std::string::size_type sz; // (1) sz = "alignas"s.find_first_of("klmn"s); // └────────────────────────┘ assert(sz == 1); sz = "alignof"s.find_first_of("wxyz"s); // 无匹配 assert(sz == std::string::npos); // (2) sz = "consteval"s.find_first_of("xyzabc", 0, 3); // 无匹配 (× 不是目标字符) ××× assert(sz == std::string::npos); sz = "consteval"s.find_first_of("xyzabc", 0, 6); // └───────────────────────────────┘ assert(sz == 0); // (3) sz = "decltype"s.find_first_of("xyzabc"); // └────────────────────────────┘ assert(sz == 2); // (4) sz = "co_await"s.find_first_of('a'); // └──────────────────────┘ assert(sz == 3); // (5) sz = "constinit"s.find_first_of("int"sv); // └─────────────────────────┘ assert(sz == 2); std::cout << "All tests passed.\n"; }
输出:
All tests passed.
缺陷报告
以下行为变更缺陷报告被追溯应用于先前发布的C++标准。
| 缺陷报告 | 适用标准 | 发布时的行为 | 正确行为 |
|---|---|---|---|
| LWG 847 | C++98 | 未提供异常安全保证 | 增加强异常安全保证 |
| LWG 2064 | C++11 | 重载 (3,4) 为 noexcept | 已移除 |
| LWG 2946 | C++17 | 重载 (5) 在某些情况下导致歧义 | 通过模板化避免歧义 |
| P1148R0 |
C++11
C++17 |
重载
(4,5)
的 noexcept 因
LWG2064/LWG2946 被意外丢弃 |
已恢复 |
参见
|
查找给定子串的首次出现
(公开成员函数) |
|
|
查找子串的最后一次出现
(公开成员函数) |
|
|
查找字符的首个缺失位置
(公开成员函数) |
|
|
查找字符的最后一次出现
(公开成员函数) |
|
|
查找字符的最后缺失位置
(公开成员函数) |
|
|
查找字符的首次出现
(
std::basic_string_view<CharT,Traits>
的公开成员函数)
|
|
|
返回仅包含另一字节字符串中字符的最大初始段长度
(函数) |