std::basic_string<CharT,Traits,Allocator>::find_first_not_of
ja.cppreference.net より
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
find_first_not_of( const StringViewLike& t,
size_type pos = 0 ) const noexcept(/* see below */);
|
(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 を sv への文字列ビューに暗黙的に変換し(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 である場合にのみオーバーロード解決に参加します。すべての場合において、等しいかどうかのチェックは Traits::eq を呼び出すことによって行われます。
パラメータ
| str | - | 検索対象の文字列を識別する文字列 |
| pos | - | 検索を開始する位置 |
| count | - | 検索対象の文字列の長さ |
| s | - | 検索対象の文字列を識別する文字列へのポインタ |
| ch | - | 検索対象の文字を識別する文字 |
| t | - | 検索対象の文字を識別するオブジェクト( std::basic_string_view に変換可能) |
戻り値
見つかった文字の位置、または std::string::npos そのような文字が見つからない場合。
例外
1,4)
例外を送出しない。
5)
noexcept
noexcept指定:
noexcept
(
std::
is_nothrow_convertible_v
<
const
T
&
,
std::
basic_string_view
<
CharT, Traits
>>
)
何らかの理由で例外がスローされた場合、この関数は何も効果を持ちません( strong exception safety guarantee )。
例
このコードを実行
#include <iostream> #include <string> int main() { // マクロ名で大文字、小文字、数字を許可 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(); // 現在 first は '%' の位置、last は見つかった部分文字列の終端の次を指す 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 member function)
|