溫馨提示×

c++中rfind函數的用法是什么

c++
小億
131
2024-05-13 17:23:17
欄目: 編程語言

在C++中,rfind函數用于在字符串中查找指定子字符串最后一次出現的位置。它的語法如下:

size_t rfind (const string& str, size_t pos = npos) const;

其中,str為要查找的子字符串,pos為從哪個位置開始查找,默認為字符串末尾。

該函數返回子字符串在原字符串中最后一次出現的位置的索引,如果未找到則返回string::npos。

示例:

#include <iostream>
#include <string>

int main() {
    std::string str = "hello world";
    size_t pos = str.rfind("world");
    
    if (pos != std::string::npos) {
        std::cout << "Substring found at position: " << pos << std::endl;
    } else {
        std::cout << "Substring not found" << std::endl;
    }
    
    return 0;
}

在上面的例子中,rfind函數會查找字符串"world"在"hello world"中最后一次出現的位置,輸出為"Substring found at position: 6"。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女