📜  C++ 中的 std:: 字符串::rfind 示例

📅  最后修改于: 2021-09-03 13:45:28             🧑  作者: Mango

std:: 字符串::rfind是一个字符串类成员函数,用于搜索字符串中任何字符的最后一次出现。如果字符出现在字符串中则返回字符串否则它会返回字符串::非营利组织,其表示指针在字符串的结尾该字符最后一次出现的索引。

头文件:

#include < string >

语法 1:

rfind(char ch)
rfind(string str)

参数:此函数将给定的字符字符串作为参数,要找到其索引。

返回值:此方法返回该字符最后一次出现位置或字符串最后一次出现的第一个索引。

方案一:

下面是说明字符串::rfind(char ch) 的程序

// C++ program to demonstrate
// rfind() method
  
#include 
#include 
#include 
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);
}
输出:

方案二:

下面是说明字符串::rfind(字符串 str) 的程序

// C++ program to demonstrate
// rfind() method
  
#include 
#include 
#include 
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);
}
输出:

语法 2:

rfind(char ch, size_t position);
rfind(string s, size_t position); 

参数:该函数采用:

  • 给定的字符字符串作为参数,要找到其索引。
  • 直到执行搜索的位置

返回值:此方法返回该位置之前给定字符或字符串的最后一个匹配项的第一个字符的位置,否则返回字符串::npos

方案三:

下面是说明字符串::rfind(char ch, size_t position) 的程序:

// C++ program to illustrate the function
// string::rfind(char ch, size_t pos)
  
#include 
#include 
#include 
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);
}
输出:

程序4:

下面是说明字符串::rfind(字符串 str, size_t position) 的程序:

// C++ program to illustrate the function
// string::rfind(string str, size_t pos)
#include 
#include 
#include 
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);
}
输出:

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live