std::string rfind
时间: 2023-08-24 22:04:19 浏览: 144
std::string rfind is a member function of the std::string class in C++. It is used to find the last occurrence of a substring within a given string.
Here is an example usage of the rfind function:
```cpp
#include <iostream>
#include <string>
int main() {
std::string str = "Hello, World! This is C++.";
// Find the last occurrence of "is" in the string
size_t pos = str.rfind("is");
if (pos != std::string::npos) {
std::cout << "Last occurrence found at position: " << pos << std::endl;
} else {
std::cout << "Substring not found." << std::endl;
}
return 0;
}
```
In this example, the rfind function is used to find the last occurrence of the substring "is" in the string "Hello, World! This is C++.". If the substring is found, the function returns the position of its first character; otherwise, it returns std::string::npos (a special value indicating "not found").
阅读全文