std::basic_string<CharT,Traits,Allocator>::find
來自 cppreference.com
< cpp | string | basic_string
size_type find( const basic_string& str, size_type pos = 0 ) const; |
(1) | (C++11 起無異常丟擲) (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 起無異常丟擲) (C++20 起為 constexpr) |
template< class StringViewLike > size_type find( const StringViewLike& t, |
(5) | (C++17 起) (C++20 起為 constexpr) |
查詢第一個等於給定字元序列的子字串。搜尋從 pos 開始,即找到的子字串不能在 pos 之前的位置開始。
1) 查詢第一個等於 str 的子字串。
2) 查詢第一個等於範圍
[
s,
s + count)
的子字串。此範圍可能包含空字元。 如果
[
s,
s + count)
不是有效範圍,則行為未定義。3) 查詢第一個等於由 s 指向的字串的子字串。字串的長度由第一個空字元使用 Traits::length(s) 確定。
如果
[
s,
s + Traits::length(s))
不是有效範圍,則行為未定義。4) 查詢第一個字元 ch(根據下面的正式規則,被視為單字元子字串)。
此過載僅當 std::is_convertible_v<const StringViewLike&,
std::basic_string_view<CharT, Traits>> 為 true 且 std::is_convertible_v<const StringViewLike&, const CharT*> 為 false 時才參與過載決議。
std::basic_string_view<CharT, Traits>> 為 true 且 std::is_convertible_v<const StringViewLike&, const CharT*> 為 false 時才參與過載決議。
形式上,如果以下所有條件都為 true,則稱子字串 str 在位置 xpos 處被 找到
- xpos >= pos
- xpos + 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 << "not found! n == npos\n"; else std::cout << "found @ 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 */ // search from beginning of string n = s.find("is"); print(1, n, s); // search from position 5 n = s.find("is", 5); print(2, n, s); // find a single character n = s.find('a'); print(3, n, s); // find a single character n = s.find('q'); print(4, n, s); }
輸出
1) found @ n = 2, substr(2) = "is is a string" 2) found @ n = 5, substr(5) = "is a string" 3) found @ n = 8, substr(8) = "a string" 4) not found! 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> 的公共成員函式) | |
搜尋一個範圍的元素首次出現的位置 (函式模板) |