命名空间
变体
操作

std::basic_string_view<CharT,Traits>::find

来自 cppreference.cn
 
 
 
 
constexpr size_type find( basic_string_view v, size_type pos = 0 ) const noexcept;
(1) (since C++17)
constexpr size_type find( CharT ch, size_type pos = 0 ) const noexcept;
(2) (since C++17)
constexpr size_type find( const CharT* s, size_type pos, size_type count ) const;
(3) (since C++17)
constexpr size_type find( const CharT* s, size_type pos = 0 ) const;
(4) (since C++17)

查找首个等于给定字符序列的子字符串。

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

[编辑] 参见

查找子字符串的末次出现
(公共成员函数) [编辑]
查找字符首次出现
(公共成员函数) [编辑]
查找字符末次出现
(公共成员函数) [编辑]
查找字符首次缺失
(公共成员函数) [编辑]
查找字符末次缺失
(公共成员函数) [编辑]
查找给定子字符串的首个出现
(std::basic_string<CharT,Traits,Allocator> 的公共成员函数) [编辑]