名稱空間
變體
操作

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

來自 cppreference.com
 
 
 
 
constexpr size_type find( basic_string_view v, size_type pos = 0 ) const noexcept;
(1) (C++17 起)
constexpr size_type find( CharT ch, size_type pos = 0 ) const noexcept;
(2) (C++17 起)
constexpr size_type find( const CharT* s, size_type pos, size_type count ) const;
(3) (C++17 起)
constexpr size_type find( const CharT* s, size_type pos = 0 ) const;
(4) (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> 的公開成員函式) [編輯]