C++中rfind方法的具體使用
rfind 是 C++ 字符串類 std::string 和 std::wstring 的成員函數(shù),用于從字符串的末尾向前搜索指定的子字符串或字符。
函數(shù)原型
// 搜索整個(gè)字符串 size_type rfind(const basic_string& str, size_type pos = npos) const noexcept; size_type rfind(const CharT* s, size_type pos = npos) const; size_type rfind(CharT ch, size_type pos = npos) const noexcept; // C++17 新增:帶長(zhǎng)度的版本 size_type rfind(const CharT* s, size_type pos, size_type count) const;
參數(shù)說明
str/s/ch:要搜索的子字符串或字符pos:開始搜索的位置(從該位置向前搜索)count:要搜索的字符數(shù)(僅用于指針版本)
返回值
- 如果找到,返回匹配子串的起始位置(從 0 開始)
- 如果未找到,返回
std::string::npos
源碼解析
以下是 rfind 的簡(jiǎn)化實(shí)現(xiàn)原理:
#include <iostream>
#include <string>
#include <cstring>
// 簡(jiǎn)化的 rfind 實(shí)現(xiàn)原理
size_t simplified_rfind(const std::string& str, const std::string& pattern, size_t pos = std::string::npos) {
// 如果 pos 超過字符串長(zhǎng)度,調(diào)整到字符串末尾
if (pos >= str.length()) {
pos = str.length() - 1;
}
// 如果模式串為空,返回 pos(標(biāo)準(zhǔn)行為)
if (pattern.empty()) {
return (pos < str.length()) ? pos : str.length();
}
// 如果模式串比原字符串長(zhǎng),肯定找不到
if (pattern.length() > str.length()) {
return std::string::npos;
}
// 從 pos 開始向前搜索
for (size_t i = pos; i >= pattern.length() - 1; --i) {
bool found = true;
// 檢查當(dāng)前位置是否匹配模式串
for (size_t j = 0; j < pattern.length(); ++j) {
if (str[i - pattern.length() + 1 + j] != pattern[j]) {
found = false;
break;
}
}
if (found) {
return i - pattern.length() + 1;
}
// 處理無符號(hào)整數(shù)的下溢
if (i == 0) break;
}
return std::string::npos;
}
使用示例
#include <iostream>
#include <string>
int main() {
std::string text = "Hello, World! Hello, C++!";
// 1. 搜索子字符串
size_t pos1 = text.rfind("Hello");
std::cout << "Last 'Hello' at position: " << pos1 << std::endl; // 輸出 14
// 2. 搜索字符
size_t pos2 = text.rfind('o');
std::cout << "Last 'o' at position: " << pos2 << std::endl; // 輸出 19
// 3. 從指定位置向前搜索
size_t pos3 = text.rfind("Hello", 10);
std::cout << "Last 'Hello' before position 10: " << pos3 << std::endl; // 輸出 0
// 4. 搜索不存在的字符串
size_t pos4 = text.rfind("Python");
if (pos4 == std::string::npos) {
std::cout << "'Python' not found" << std::endl;
}
// 5. 使用指針和長(zhǎng)度
const char* search_str = "Hello";
size_t pos5 = text.rfind(search_str, text.length(), 3); // 只搜索前3個(gè)字符 "Hel"
std::cout << "Last 'Hel' at position: " << pos5 << std::endl;
return 0;
}
實(shí)際應(yīng)用場(chǎng)景
#include <iostream>
#include <string>
// 提取文件擴(kuò)展名
std::string getFileExtension(const std::string& filename) {
size_t dotPos = filename.rfind('.');
if (dotPos != std::string::npos && dotPos < filename.length() - 1) {
return filename.substr(dotPos + 1);
}
return "";
}
// 提取目錄路徑
std::string getDirectory(const std::string& path) {
size_t slashPos = path.rfind('/');
if (slashPos != std::string::npos) {
return path.substr(0, slashPos + 1);
}
return "";
}
// 查找最后一個(gè)單詞
std::string getLastWord(const std::string& sentence) {
size_t lastSpace = sentence.rfind(' ');
if (lastSpace != std::string::npos) {
return sentence.substr(lastSpace + 1);
}
return sentence; // 沒有空格,返回整個(gè)字符串
}
int main() {
std::string filename = "document.backup.txt";
std::string path = "/home/user/documents/file.txt";
std::string sentence = "The quick brown fox";
std::cout << "File extension: " << getFileExtension(filename) << std::endl;
std::cout << "Directory: " << getDirectory(path) << std::endl;
std::cout << "Last word: " << getLastWord(sentence) << std::endl;
return 0;
}
性能特點(diǎn)
- 時(shí)間復(fù)雜度:最壞情況 O(n×m),其中 n 是搜索范圍,m 是模式串長(zhǎng)度
- 空間復(fù)雜度:O(1)
- 適用場(chǎng)景:適合在長(zhǎng)字符串中查找較短的模式串
注意事項(xiàng)
rfind從后向前搜索,但返回的位置是從字符串開頭計(jì)算的- 如果
pos參數(shù)為npos,則從字符串末尾開始搜索 - 空字符串的搜索總是會(huì)成功(返回
pos或字符串長(zhǎng)度) - 注意處理
npos的返回值
rfind 是字符串處理中非常有用的工具,特別適合需要從后向前搜索的場(chǎng)景,如文件路徑處理、URL 解析等。
到此這篇關(guān)于C++中rfind方法的具體使用的文章就介紹到這了,更多相關(guān)C++ rfind內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語(yǔ)言控制臺(tái)實(shí)現(xiàn)打飛機(jī)小游戲
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言控制臺(tái)實(shí)現(xiàn)打飛機(jī)小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04
C語(yǔ)言中回調(diào)函數(shù)的含義與使用場(chǎng)景詳解
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言中回調(diào)函數(shù)的含義與使用場(chǎng)景,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-03-03
C/C++實(shí)現(xiàn)去除字符串中的空格(附帶源碼)
這篇文章主要為大家詳細(xì)介紹了C/C++實(shí)現(xiàn)去除字符串中的空格的相關(guān)方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-11-11
C++數(shù)據(jù)結(jié)構(gòu)之list詳解
list是一種序列式容器。list容器完成的功能實(shí)際上和數(shù)據(jù)結(jié)構(gòu)中的雙向鏈表是極其相似的,list中的數(shù)據(jù)元素是通過鏈表指針串連成邏輯意義上的線性表,也就是list也具有鏈表的主要優(yōu)點(diǎn),即:在鏈表的任一位置進(jìn)行元素的插入、刪除操作都是快速的2021-11-11
VC++獲得當(dāng)前進(jìn)程運(yùn)行目錄的方法
這篇文章主要介紹了VC++獲得當(dāng)前進(jìn)程運(yùn)行目錄的方法,可通過系統(tǒng)函數(shù)實(shí)現(xiàn)該功能,是非常實(shí)用的技巧,需要的朋友可以參考下2014-10-10

