當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C++ std::string::rfind用法及代碼示例

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.




相關用法


注:本文由純淨天空篩選整理自pranjal_g99大神的英文原創作品 std::string::rfind in C++ with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。