std::basic_string<CharT,Traits,Allocator>:: find
|
size_type find
(
const
basic_string
&
str, size_type pos
=
0
)
const
;
|
(1) |
(C++11以降noexcept)
(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以降noexcept)
(C++20以降constexpr) |
|
template
<
class
StringViewLike
>
size_type find
(
const
StringViewLike
&
t,
|
(5) |
(C++17以降)
(C++20以降constexpr) |
指定された文字シーケンスと等しい最初の部分文字列を検索します。検索は pos から開始され、つまり見つかった部分文字列は pos より前の位置から始まっていてはなりません。
[
s
,
s
+
count
)
と等しい最初の部分文字列を検索します。この範囲にはnull文字が含まれる可能性があります。
std:: basic_string_view < CharT, Traits >> が true であり、かつ std:: is_convertible_v < const StringViewLike & , const CharT * > が false である場合です。
形式的には、部分文字列 str は、以下の条件がすべて true である場合に位置 xpos で 見つかった とされます:
- xpos >= pos
- xpos + str. size ( ) <= size ( )
- str 内の全ての位置 n について、 Traits :: eq ( at ( xpos + n ) , str. at ( n ) ) が成り立つ。
特に、これは以下を意味します。
- 部分文字列が見つかるのは、 pos <= size ( ) - str. size ( ) の場合のみです。
- 空の部分文字列は、 pos の位置で見つかるのは、 pos <= size ( ) の場合に限ります。
- 空でない部分文字列の場合、 pos >= size ( ) の場合、関数は常に npos を返します。
目次 |
パラメータ
| str | - | 検索対象の文字列 |
| pos | - | 検索を開始する位置 |
| count | - | 検索する部分文字列の長さ |
| s | - | 検索対象の文字列へのポインタ |
| ch | - | 検索対象の文字 |
| t | - | 検索対象のオブジェクト( std::basic_string_view に変換可能) |
戻り値
見つかった部分文字列の最初の文字の位置、または npos そのような部分文字列が見つからない場合。
例外
何らかの理由で例外がスローされた場合、この関数は何も効果を持ちません( strong exception safety guarantee )。
例
#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 */ // 文字列の先頭から検索 n = s.find("is"); print(1, n, s); // 位置5から検索 n = s.find("is", 5); print(2, n, s); // 単一文字を検索 n = s.find('a'); print(3, n, s); // 単一文字を検索 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++規格に対して遡及的に適用されました。
| DR | 適用対象 | 公開時の仕様 | 正しい仕様 |
|---|---|---|---|
| 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>
の公開メンバ関数)
|
|
|
要素範囲の最初の出現を検索する
(関数テンプレート) |