- string[meta header]
- std[meta namespace]
- basic_string[meta class]
- function[meta id-type]
size_type rfind(const basic_string& str, size_type pos = npos) const; // (1) C++03
size_type rfind(const basic_string& str, size_type pos = npos) const noexcept; // (1) C++11
size_type rfind(const charT* s, size_type pos, size_type n) const; // (2)
size_type rfind(const charT* s, size_type pos = npos) const; // (3)
size_type rfind(charT c, size_type pos = npos) const; // (4)
size_type rfind(std::basic_string_view<charT, traits> sv,
size_type pos = npos) const noexcept; // (5) C++17
最後に現れる指定文字列を検索する。
(3) の形式の場合、s
は少なくとも traits_type::length(s) + 1
の要素を持つ charT
の配列を指していること。
- (1)
pos
より前で最後にstr
と一致する位置を返す。 - (2)
pos
より前で最後にs
と一致する位置を返す。s
は長さn
の文字列へのポインタである。 - (3) (2) と同様だが、こちらは NULL 終端の文字列を扱う。
- (4)
pos
より前で最後にc
と一致する位置を返す。 - (5)
pos
より前で最後にsv
と一致する位置を返す。
見つかればその位置を返し、見つからない場合は basic_string::npos
を返す。
- (1) 投げない
- (5) 投げない
pos
は比較対象となる最後の文字位置では無いことに注意。(例を参照)- 文字列の一致は、文字列の各文字について
traits_type::eq
を用いて検査される。
例えば、(1) の形式の場合、以下のような条件を満たす最大のxpos
を求める。xpos <= pos
かつxpos + str.size() <= size()
0 <= I
かつI < str.size()
を満たす全てのI
について、traits_type::eq(at(xpos + I), str.at(I))
- (3) の形式の場合、
s
の文字列長はtraits_type::length(s)
で求められる。
#include <iostream>
#include <string>
int main()
{
const std::string s("Hello, world. Welcome to C++ world.");
const std::string str("world");
// pos を後ろの "world" の "w" の位置にしても、後ろの "world" が見つかる
std::cout << s.rfind(str, 29) << std::endl;
// pos を後ろの "world" の "w" の一つ前にすると、後ろの "world" は見つからずに前の "world" が見つかる
std::cout << s.rfind("world", 28) << std::endl;
// 標準の char_traits では大文字と小文字は区別されるため、"world" の "w" は見つからずに "Welcome" の "W" が見つかる
std::cout << s.rfind('W', 29) << std::endl;
}
- rfind[color ff0000]
29
7
14
(1) 以外の形式は、(1) の形式を使用して実装することができる。
// (2)
template <class charT, class traits, class Allocator>
size_type basic_string<charT, traits, Allocator>::rfind(const charT* s, size_type pos, size_type n) const
{
return rfind(std::basic_string(s, n), pos);
}
// (3)
template <class charT, class traits, class Allocator>
size_type basic_string<charT, traits, Allocator>::rfind(const charT* s, size_type pos = npos) const
{
return rfind(std::basic_string(s), pos);
}
// (4)
template <class charT, class traits, class Allocator>
size_type basic_string<charT, traits, Allocator>::rfind(charT c, size_type pos = npos) const
{
return rfind(std::basic_string(1, c), pos);
}
名前 | 説明 |
---|---|
find_end |
指定された最後のサブシーケンスを検索する |