std::string_view find() 方法
- 自 C++17 起
// (1) Const version only
constexpr size_type find( basic_string_view v, size_type pos = 0 ) const noexcept;
// (2) Const version only
constexpr size_type find( CharT ch, size_type pos = 0 ) const noexcept;
// (3) Const version only
constexpr size_type find( const CharT* s, size_type pos, size_type count ) const;
// (4) Const version only
constexpr size_type find( const CharT* s, size_type pos = 0 ) const;
查找第一个等于给定字符序列的子字符串。
- (1) 在此视图中查找
v
的第一个出现,从位置pos
开始。 - (2) 等同于
find(basic_string_view(std::addressof(ch), 1), pos)
。 - (3) 等同于
find(basic_string_view(s, count), pos)
。 - (4) 等同于
find(basic_string_view(s), pos)
。
参数
v
- 要搜索的视图pos
- 开始搜索的位置count
- 要搜索的子字符串的长度s
- 指向要搜索的字符字符串的指针ch
- 要搜索的字符
返回值
找到的子字符串的第一个字符的位置,如果未找到此类子字符串,则为 npos
。
复杂度
最坏情况下为 O(size() * v.size())。
重要
本节需要改进。您可以通过编辑此文档页面来帮助我们。
异常
(无)
示例
#include <string_view>
int main()
{
using namespace std::literals;
constexpr auto str{" long long int;"sv};
static_assert(
1 == str.find("long"sv) && "<- find(v , pos = 0)" &&
6 == str.find("long"sv, 2) && "<- find(v , pos = 2)" &&
0 == str.find(' ') && "<- find(ch, pos = 0)" &&
2 == str.find('o', 1) && "<- find(ch, pos = 1)" &&
2 == str.find("on") && "<- find(s , pos = 0)" &&
6 == str.find("long double", 5, 4) && "<- find(s , pos = 5, count = 4)"
);
static_assert(str.npos == str.find("float"));
}