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,
|
(5) |
(C++17以降)
(C++20以降constexpr) |
指定された文字シーケンスと等しい最後の部分文字列を検索します。検索は pos から始まり、右から左に向かって進みます(したがって、見つかった部分文字列がある場合、 pos より後の位置から始まることはできません)。 npos または size() - 1 以上の値が pos として渡された場合、文字列全体が検索されます。
[
s
,
s
+
count
)
に等しい最後の部分文字列を検索します。この範囲にはnull文字を含めることができます。
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)
|