Syntax:
#include <string> size_type rfind( const string& str, size_type index = npos ) const; size_type rfind( const charT* str, size_type index = npos ) const; size_type rfind( const charT* str, size_type index, size_type num ) const; size_type rfind( charT ch, size_type index = npos ) const;
rfind()関数は、検索文字列を”index”から逆順に文字列を検索し、最後に見つかった位置を返します。
次のサンプルコードでは最初にrfind()が呼ばれたときには、ターゲットとなる単語が9文字目にあるためにstring::nposが返されます。 しかし、文字列の20番目までには含まれるため、二回目に呼ばれたときには9が返ります。
string::size_type loc; string s = "My cat's breath smells like cat food."; loc = s.rfind( "breath", 8 ); cout << "The word breath is at index " << loc << endl; loc = s.rfind( "breath", 20 ); cout << "The word breath is at index " << loc << endl;
Related Topics: find, find_first_not_of, find_first_of, find_last_not_of, find_last_of