std::basic_string<CharT,Traits,Allocator>::find
来自cppreference.com
<tbody>
</tbody>
size_type find( const basic_string& str, size_type pos = 0 ) const; |
(1) | (C++11 起为 noexcept) (C++20 起为 constexpr) |
size_type find( const CharT* s, size_type pos, size_type count ) const; |
(2) | (C++20 起为 constexpr) |
size_type find( const CharT* s, size_type pos = 0 ) const; |
(3) | (C++20 起为 constexpr) |
size_type find( CharT ch, size_type pos = 0 ) const; |
(4) | (C++11 起为 noexcept) (C++20 起为 constexpr) |
template< class StringViewLike > size_type find( const StringViewLike& t, size_type pos = 0 ) const noexcept(/* 见下文 */); |
(5) | (C++17 起) (C++20 起为 constexpr) |
寻找首个等于给定字符序列的子串。搜索从 pos 开始,也就是说找到的子串不会从 pos 之前的位置开始。
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 时才会参与重载决议。
正式而言,如果以下表达式都是 true,那么在位置 xpos 找到 子串 str:
xpos >= posxpos + str.size() <= size()- 对于
str中所有位置n,Traits::eq(at(xpos + n), str.at(n))
特别是,这意味着
- 只有在
pos <= size() - str.size()时才能找到子串。 - 在且仅在
pos <= size()时才能在pos找到空子串。 - 对于非空子串,如果
pos >= size(),那么函数始终返回 npos。
参数
| 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>>)如果因为任何原因抛出了异常,那么此函数无效果(强异常安全保证)。
示例
运行此代码
#include <iomanip>
#include <iostream>
#include <string>
void print(int id, std::string::size_type n, std::string const& s)
{
std::cout << id << ") ";
if (std::string::npos == n)
std::cout << "没有找到!n == npos\n";
else
std::cout << "在位置 n = " << n << " 找到,substr(" << n << ") = "
<< std::quoted(s.substr(n)) << '\n';
}
int main()
{
std::string::size_type n;
std::string const s = "This is a string"; /*
^ ^ ^
1 2 3 */
// 从首个位置开始搜索
n = s.find("is");
print(1, n, s);
// 从位置 5 开始搜索
n = s.find("is", 5);
print(2, n, s);
// 寻找单个字符
n = s.find('a');
print(3, n, s);
// 寻找单个字符
n = s.find('q');
print(4, n, s);
}
输出:
1) 在位置 n = 2 找到,substr(2) = "is is a string"
2) 在位置 n = 5 找到,substr(5) = "is a string"
3) 在位置 n = 8 找到,substr(8) = "a string"
4) 没有找到!n == npos
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 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> 的公开成员函数)
| |
| 搜索元素范围的首次出现 (函数模板) |