std::basic_string<CharT,Traits,Allocator>::find_first_of
来自cppreference.com
<tbody>
</tbody>
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(/* 见下文 */); |
(5) | (C++17 起) (C++20 起为 constexpr) |
寻找首个等于给定字符序列中字符之一的字符。搜索只考虑范围 [pos, size())。如果范围中不存在给定的任何一个字符,那么就会返回 npos。
1) 寻找首个等于
str 中字符之一的字符。2) 寻找首个等于范围
[s, s + count) 中字符中字符之一的字符。此范围可以包含空字符。3) 寻找首个等于
s 所指向的字符串中字符之一的字符。该字符串的长度由首个空字符,通过 Traits::length(s) 确定。4) 寻找首个等于
ch 的字符。5) 如同用
std::basic_string_view<CharT, Traits> sv = t; 将 t 隐式转换到字符串视图 sv ,然后寻找首个等于 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 << "通过所有测试。\n";
}
输出:
通过所有测试。
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 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> 的公开成员函数)
| |
| 返回仅由另一字节字符串中找到的字符组成的最大起始段的长度 (函数) |