The Wayback Machine - https://web.archive.org/web/20180530161003/http://ja.cppreference.com:80/w/cpp/string/basic_string/rfind
名前空間
変種
操作

std::basic_string::rfind

提供: cppreference.com
< cpp‎ | string‎ | basic string
 
 
 
std::basic_string
 
size_type rfind(const basic_string& str, size_type pos = npos )const;
(1)
size_type rfind(const CharT* s, size_type pos, size_type count )const;
(2)
size_type rfind(const CharT* s, size_type pos = npos )const;
(3)
size_type rfind( CharT ch, size_type pos = npos )const;
(4)
template<class T >
size_type rfind(const T& t, size_type pos = npos )const;
(5) (C++17およびそれ以降)

指定された文字列と等しい最後の部分文字列を探します。 検索は pos から開始されます。 つまり、見つかった文字列が位置 pos より後の位置で始まることはありません。 pos として npos または size()-1 より小さくない任意の値が渡された場合は、文字列全体が検索されます。

1)rfind(std::basic_string_view<CharT, Traits>(str), pos) によって行われたかのように、(C++17およびそれ以降)str と等しい最後の部分文字列を探します (等しさは Traits::eq を呼ぶことによって調べられます)。
2)s の指す文字列の最初の count 個の文字と等しい最後の部分文字列を探します。 s はNULL文字を含むことができます。 rfind(std::basic_string(s, count), pos)(C++17以前)rfind(std::basic_string_view<CharT, Traits>(s, count), pos)(C++17およびそれ以降) と同等です。
3)s の指す文字列と等しい最後の部分文字列を探します。 文字列の長さは最初のNULL文字によって決定されます。 rfind(std::basic_string(s), pos)(C++17以前)rfind(std::basic_string_view<CharT, Traits>(s), pos)(C++17およびそれ以降) と同等です。
4)ch と等しい最後の文字を探します。 rfind(std::basic_string(1,c),pos) と同等です。
5)std::basic_string_view<CharT, Traits> sv = t; によって行われたかのように、 t を文字列ビュー sv に暗黙に変換し、 sv の内容と等しい最後の部分文字列を探します。 このオーバーロードは、std::is_convertible_v<const T&, std::basic_string_view<CharT, Traits>>true であり、 std::is_convertible_v<const T&, const CharT*>false である場合にのみ、オーバーロード解決に参加します。

目次

[編集]引数

str - 検索する文字列
pos - 検索を開始する位置
count - 検索する部分文字列の長さ
s - 検索する文字列を指すポインタ
ch - 検索する文字
t - 検索する (std::basic_string_view に変換可能な) オブジェクト

[編集]戻り値

見つかった部分文字列の最初の文字の位置、またはそのような部分文字列が見つからない場合は npos。 これは文字列の開始位置であることに注意してください。 終了位置ではありません。

空文字列 (str.size()count、または strlen(s) がゼロ) を検索した場合は pos を返します (空文字列はただちに見つかります)。 ただし pos > size() (pos == npos も含みます) の場合は size() を返します。

それ以外で size() がゼロの場合は常に npos が返されます。

[編集]例外

1-4) (なし)
(C++11以前)
1,4)
noexcept 指定:  
noexcept
  
2,3) (なし)
(C++11およびそれ以降)
(C++14以前)
1)
noexcept 指定:  
noexcept
  
2,3,4) (なし)
(C++14およびそれ以降)
5)sv の初期化が例外を投げなければ、何も投げません。
(C++17およびそれ以降)

[編集]欠陥報告

以下の動作変更欠陥報告は以前に発行された C++ 標準に遡って適用されました。

DR 適用先 発行時の動作 正しい動作
LWG 2946 C++17 string_view overload causes ambiguity in some cases avoided by making it a template

[編集]

#include <string>#include <iostream>   void print(std::string::size_type n, std::stringconst&s){if(n ==std::string::npos){std::cout<<"not found\n";}else{std::cout<<"found: \""<< s.substr(n)<<"\" at "<< n <<'\n';}}   int main(){ std::string::size_type n;std::stringconst s ="This is a string";   // search backwards from end of string n = s.rfind("is"); print(n, s);// search backwards from position 4 n = s.rfind("is", 4); print(n, s);// find a single character n = s.rfind('s'); print(n, s);// find a single character n = s.rfind('q'); print(n, s);}

出力:

found: "is a string" at 5 found: "is is a string" at 2 found: "string" at 10 not found

[編集]関連項目

文字列内の文字を探します
(パブリックメンバ関数)[edit]
文字が現れる最初の位置を探します
(パブリックメンバ関数)[edit]
文字が現れない最初の位置を探します
(パブリックメンバ関数)[edit]
文字が現れる最後の位置を探します
(パブリックメンバ関数)[edit]
文字が現れない最後の位置を探します
(パブリックメンバ関数)[edit]
close