在线文档教程
C++
字符串 | Strings

std::basic_string::rfind

性病:基本[医]字符串:Rfind

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)
size_type rfind( std::basic_string_view<CharT, Traits> sv, size_type pos = npos) const;(5)(since C++17)

查找最后一个子字符串,该子字符串等于给定的字符序列。搜索开始于pos,即发现的子字符串不能以下面的位置开始。pos.如果npos或任何不小于size()-1作为pos,整个字符串将被搜索。

1%29找到最后一个等于str%28相等性是通过调用特征:情商%29,似乎rfind(std::basic_string_view<CharT, Traits>(str), pos)%28自C++17%29

2%29查找最后一个子字符串,该子字符串与第一个子字符串相同。count所指向的字符串的字符。s...s可以包含空字符。相当于rfind(std::basic_string(s, count), pos)%28直到C++17%29rfind(std::basic_string_view<CharT, Traits>(s, count), pos)%28自C++17%29

3%29查找最后一个子字符串,该子字符串等于s字符串的长度由第一个空字符决定。相当于rfind(std::basic_string(s), pos)%28直到C++17%29rfind(std::basic_string_view<CharT, Traits>(s), pos)%28自C++17%29

4%29找到最后一个字符ch相当于rfind(std::basic_string(1,c),pos)

5%29查找最后一个子字符串,该子字符串等于sv

参数

str-string to search for
pos-position at which to begin searching
count-length of substring to search for
s-pointer to a character string to search for
ch-character to search for
sv-std::basic_string_view to search for

返回值

找到的子字符串或npos如果找不到这样的子字符串。注意,这是一个从字符串开始的偏移,而不是结束。

如果size()是零,npos总是会被归还。

如果搜索空字符串%28str.size(),,,count,或strlen(s)为零%29返回pos%28空字符串立即找到%29,除非pos == npos,在这种情况下返回size()...

例外

1-4) (none)(until C++11)
1,4) noexcept specification: noexcept 2,3) (none)(since C++11)(until C++14)
1) noexcept specification: noexcept 2,3,4) (none)(since C++14)
5) noexcept specification: noexcept(since C++17)

二次

#include <string> #include <iostream> void print(std::string::size_type n, std::string const &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::string const 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

二次

另见

findfind characters in the string (public member function)
find_first_offind first occurrence of characters (public member function)
find_first_not_offind first absence of characters (public member function)
find_last_offind last occurrence of characters (public member function)
find_last_not_offind last absence of characters (public member function)

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/string/basic[医]字符串/rfind