std::basic_string<CharT,Traits,Allocator>::find_first_not_of
From cppreference.com
< cpp | string | basic string
size_type find_first_not_of( const basic_string& str, size_type pos = 0 ) const; |
(1) | (C++11 以降 noexcept) (C++20 以降 constexpr) |
size_type find_first_not_of( const CharT* s, size_type pos, size_type count ) const; |
(2) | (C++20 以降 constexpr) |
size_type find_first_not_of( const CharT* s, size_type pos = 0 ) const; |
(3) | (C++20 以降 constexpr) |
size_type find_first_not_of( CharT ch, size_type pos = 0 ) const; |
(4) | (C++11 以降 noexcept) (C++20 以降 constexpr) |
template< class StringViewLike > size_type |
(5) | (C++17以降) (C++20 以降 constexpr) |
指定された文字シーケンスのいずれの文字にも一致しない最初の文字を検索します。検索は範囲 [pos, size()) のみ考慮します。範囲内のすべての文字が指定された文字シーケンス内で見つかった場合、npos が返されます。
1) str のいずれの文字にも一致しない最初の文字を検索します。
2) 範囲
[s, s + count) のいずれの文字にも一致しない最初の文字を検索します。この範囲にはヌル文字が含まれる場合があります。 範囲
[s, s + count)が有効な範囲でない場合、動作は未定義です。3) s が指す文字文字列のいずれの文字にも一致しない最初の文字を検索します。文字列の長さは、最初のヌル文字によって
Traits::length(s) として決定されます。 範囲
[s, s + Traits::length(s))が有効な範囲でない場合、動作は未定義です。4) ch と等しくない最初の文字を検索します。
5) t を、std::basic_string_view<CharT, Traits> sv = t; とした場合と同様に、文字列ビューに暗黙的に変換し、その sv のいずれの文字にも一致しない最初の文字を検索します。
このオーバーロードは、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 である場合にのみ、オーバーロード解決に参加します。
すべての場合において、等価性はTraits::eqを呼び出すことによってチェックされます。
目次 |
[編集] パラメータ
| str | - | 検索対象の文字を識別する文字列 |
| pos | - | 検索を開始する位置 |
| count | - | 検索対象の文字文字列の長さ |
| s | - | 検索対象の文字文字列を指すポインタ |
| 文字 | - | 検索対象の文字を特定する文字 |
| t | - | std::basic_string_view に変換可能なオブジェクトで、検索対象の文字を識別するもの |
[編集] 戻り値
見つかった文字の位置、またはそのような文字が見つからなかった場合は std::string::npos。
[編集] 例外
1,4) 何も投げません。
5)
noexcept 指定:
noexcept(std::is_nothrow_convertible_v<const T&, std::basic_string_view<CharT, Traits>>)
何らかの理由で例外がスローされた場合、この関数は効果がありません(強力な例外安全保証)。
[編集] 例
このコードを実行
#include <iostream> #include <string> int main() { // Permit uppercase letters, lowercase letters and numbers in macro names const char* pattern = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz" "0123456789"; std::string data = "1) %FIX, 2) %HACK, and 3) %TODO"; const std::string replacement = "%DONE%"; std::cout << "Before: " << data << '\n'; for (std::string::size_type first{}, last{}; (first = data.find('%', first)) != std::string::npos; first += replacement.size()) { last = data.find_first_not_of(pattern, first + 1); if (last == std::string::npos) last = data.length(); // Now first at '%' and last is one past end of the found substring data.replace(first, last - first, replacement); } std::cout << "After: " << data << '\n'; }
出力
Before: 1) %FIX, 2) %HACK, and 3) %TODO After: 1) %DONE%, 2) %DONE%, and 3) %DONE%
[編集] 不具合報告
以下の動作変更を伴う欠陥報告が、以前に公開された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 により偶発的に削除されました。 |
復元されました。 |
[編集] 関連項目
| 指定された部分文字列が最初に現れる位置を見つける (public member function) | |
| 部分文字列が最後に現れる位置を見つける (public member function) | |
| 文字が最初に現れる位置を見つける (public member function) | |
| 文字が最後に現れる位置を見つける (public member function) | |
| 文字が最後に現れない位置を見つける (public member function) | |
| 文字が最初に現れない位置を見つける ( std::basic_string_view<CharT,Traits> の public メンバ関数) |