std::basic_string_view<CharT,Traits>::find
出自cppreference.com
| |
(1) | (C++17 起) |
| |
(2) | (C++17 起) |
| |
(3) | (C++17 起) |
| |
(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> 的公開成員函數)
|