std::string::rfind是一个字符串类成员函数,用于搜索字符串中任何字符的最后出现。如果字符存在于字符串中,则它返回该字符在字符串中最后一次出现的索引,否则它将返回string::npos,它指示指针位于字符串的末尾。
头文件:
#include < string >
语法1:
rfind(char ch) rfind(string str)
参数:此函数将给定字符或字符串作为参数,以找到其索引。
返回值:此方法返回该字符最后一次出现的位置或字符串最后一次出现的第一个索引。
程序1:
下面是说明string::rfind(char ch的程序:
// C++ program to demonstrate
// rfind() method
#include <cstddef>
#include <iostream>
#include <string>
using namespace std;
// Function to return last occurrence
// of character in a string
void findLastOccurernce(string str, char ch)
{
// To store the index of the result
size_t found;
// Function to find the last
// occurence of character ch
// in string str
found = str.rfind(ch);
// If string doesn't have
// character ch present in it
if (found == string::npos) {
cout << "Character " << ch
<< " is not present in"
<< " the given string.";
}
// Else print the position
else {
cout << "The last occurence of '"
<< ch << "' is found at index:"
<< found << endl;
}
}
// Driver Code
int main()
{
// Given String
string str("Welcome to GeeksforGeeks!");
// Character to be found
char ch = 'e';
findLastOccurernce(str, ch);
}
The last occurence of ‘e’ is found at index:21
程序2:
下面是说明string::rfind(string str的程序:
// C++ program to demonstrate
// rfind() method
#include <cstddef>
#include <iostream>
#include <string>
using namespace std;
// Function to return last occurrence
// of string in a string
void findLastOccurernce(string str, string s)
{
// To store the index of the result
size_t found;
// Function to find the first index
// of last occurence of string s in str
found = str.rfind(s);
// If string doesn't have
// string s present in it
if (found == string::npos) {
cout << "String '" << s
<< "' is not present in"
<< " the given string.";
}
// Else print the position
else {
cout << "The first index of last "
<< "occurence of '" << s
<< "' is found at index:"
<< found << endl;
}
}
// Driver Code
int main()
{
// Given String
string str("Welcome to GeeksforGeeks!");
// string to be found
string s = "to";
findLastOccurernce(str, s);
}
The first index of last occurence of ‘to’ is found at index:8
语法2:
rfind(char ch, size_t position); rfind(string s, size_t position);
参数:该函数需要:
- 给定字符或字符串作为参数,将找到其索引。
- 直到执行搜索的位置。
返回值:此方法返回给定字符或字符串的最后一个匹配项的第一个字符在该位置之前的位置,否则返回string::npos
程序3:
下面是说明string::rfind(char ch,size_t位置的程序:
// C++ program to illustrate the function
// string::rfind(char ch, size_t pos)
#include <cstddef>
#include <iostream>
#include <string>
using namespace std;
// Function to return last occurrence
// of character in a string
void findLastOccurernce(
string str, char ch, size_t position)
{
// To store the index of the result
size_t found;
// Function to find the last occurence
// of character ch in str before pos 5
found = str.rfind(ch, position);
// If string doesn't have
// character ch present in it
if (found == string::npos) {
cout << "Character " << ch
<< " is not present in"
<< " the given string.";
}
// Else print the position
else {
cout << "The last occurence of "
<< ch << " before position "
<< position
<< " is found at index:"
<< found << endl;
}
}
// Driver Code
int main()
{
// Given String
string str("Welcome to GeeksforGeeks!");
// Character to be found
char ch = 'e';
// Position till where
// the search is to be done
size_t position = 5;
findLastOccurernce(str, ch, position);
}
The last occurence of e before position 5 is found at index:1
程序4:
下面是说明string::rfind(string str,size_t位置的程序:
// C++ program to illustrate the function
// string::rfind(string str, size_t pos)
#include <cstddef>
#include <iostream>
#include <string>
using namespace std;
// Function to return last occurrence
// of string in a string
void findLastOccurernce(
string str, string s, size_t position)
{
// To store the index of result
size_t found;
// Function to find the last occurence of
// string s in str before given position
found = str.rfind(s, position);
// If string doesn't have
// string s present in it
// If string doesn't have
// string s present in it
if (found == string::npos) {
cout << "String " << s
<< " is not present in"
<< " the given string.";
}
// Else print the position
else {
cout << "The last occurence of "
<< s << " before position "
<< position
<< " is found at index:"
<< found << endl;
}
}
// Driver Code
int main()
{
// Given String
string str("Welcome to GeeksforGeeks!");
// string to be found
string s = "to";
// Position till where
// the search is to be done
size_t position = 5;
findLastOccurernce(str, s, position);
}
String to is not present in the given string.
相关用法
- C语言 strtok()、strtok_r()用法及代码示例
- C语言 memset()用法及代码示例
- C++ std::mismatch()用法及代码示例
- C++ wcscpy()用法及代码示例
- C++ wcscmp()用法及代码示例
- C++ ratio_equal()用法及代码示例
- C++ std::equal_to用法及代码示例
- C++ quick_exit()用法及代码示例
- C++ multiset lower_bound()用法及代码示例
- C++ multiset upper_bound()用法及代码示例
- C++ multiset max_size()用法及代码示例
- C++ forward_list max_size()用法及代码示例
- C++ std::allocator()用法及代码示例
- C++ array data()用法及代码示例
- C++ multiset size()用法及代码示例
- C++ ratio_not_equal()用法及代码示例
注:本文由纯净天空筛选整理自pranjal_g99大神的英文原创作品 std::string::rfind in C++ with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。