📜  std :: 字符串:: rfind在C++中包含示例

📅  最后修改于: 2021-05-30 19:17:24             🧑  作者: Mango

std :: 字符串:: rfind是一个字符串类成员函数,用于搜索字符串中任何字符的最后出现。如果字符存在于字符串,则它将返回该字符在字符串最后一次出现的索引,否则它将返回字符串:: npos ,这表示指针位于字符串的末尾。

头文件:

#include < string >

语法1:

rfind(char ch)
rfind(string str)

参数:该函数将给定字符字符串作为参数,以找到其索引。

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

程序1:

下面是说明字符串:: 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);
}
输出:

程式2:

下面是说明字符串:: 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

程序3:

下面是说明字符串:: 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位置)的程序:

// 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);
}
输出:
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”