std::basic_string<CharT,Traits,Allocator>::rfind
size_type rfind( const basic_string& str, size_type pos = npos ) const;
|
(1) | (C++11 から noexcept) (C++20 から constexpr) |
size_type rfind( const CharT* s, size_type pos, size_type count ) const;
|
(2) | (C++20 から constexpr) |
size_type rfind( const CharT* s, size_type pos = npos ) const;
|
(3) | (C++20 から constexpr) |
size_type rfind( CharT ch, size_type pos = npos ) const;
|
(4) | (C++11 から noexcept) (C++20 から constexpr) |
template< class StringViewLike >
size_type rfind( const StringViewLike& t,
size_type pos = npos ) const noexcept(/* see below */);
|
(5) | (C++17 から) (C++20 から constexpr) |
指定された文字シーケンスと等しい最後の部分文字列を探します。検索は pos から始まり、右から左へ進みます(そのため、見つかった部分文字列は、もしあれば、pos の後の位置から始まることはありません)。npos または size()- 1 より小さくない値が pos として渡された場合、文字列全体が検索されます。
str と等しい最後の部分文字列を探します。[s, s + count) と等しい最後の部分文字列を探します。この範囲にはヌル文字が含まれる可能性があります。[s, s + count) が 有効な範囲でない場合、動作は未定義です。s が指す文字列と等しい最後の部分文字列を探します。文字列の長さは、Traits::length(s) を使用して最初のヌル文字によって決定されます。[s, s + Traits::length(s)) が 有効な範囲でない場合、動作は未定義です。ch と等しい最後の文字を探します。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 に変換可能) |
戻り値
見つかった部分文字列の最初の文字の位置、または npos そのような部分文字列が見つからない場合。これは文字列の末尾ではなく、先頭からのオフセットであることに注意してください。
空の文字列を検索する場合(すなわち、
str.
size
(
)
、
count
または
Traits
::
length
(
s
)
がゼロの場合)、空の文字列は直ちに見つかり、
rfind
は以下を返します:
- pos 、もし pos < size ( ) の場合;
- size ( ) それ以外の場合、 pos == npos の場合を含む。
それ以外の場合、 size() がゼロの場合、 npos が常に返されます。
例外
何らかの理由で例外がスローされた場合、この関数は何も効果を持ちません( strong exception safety guarantee )。
例
#include <iomanip> #include <iostream> #include <string> void print(std::string::size_type n, std::string::size_type len, std::string const &s) { if (n == std::string::npos) std::cout << "not found\n"; else std::cout << "found: " << std::quoted(s.substr(n, len)) << " at " << n << '\n'; } int main() { std::string::size_type n; std::string const s = "This is a string"; // 文字列の末尾から後方検索 n = s.rfind("is"); print(n, 2, s); // 位置4から後方検索 n = s.rfind("is", 4); print(n, 2, s); // 単一文字を検索 n = s.rfind('s'); print(n, 1, s); // 単一文字を検索 n = s.rfind('q'); print(n, 1, s); // プレフィックスを検索 (s.starts_with("This")も参照) n = s.rfind("This", 0); print(n, 4, s); }
出力:
found: "is" at 5 found: "is" at 2 found: "s" at 10 not found found: "This" at 0
不具合報告
以下の動作変更の欠陥報告書は、以前に公開された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)
|