#include <iostream>
#include <string>
int main() {
std::string str = "Hello, world!";
std::string substr = "world";
// 使用 string.find 查找子字符串的位置
size_t found = str.find(substr);
if (found != std::string::npos) {
std::cout << "Substring found at position: " << found << std::endl;
} else {
std::cout << "Substring not found" << std::endl;
}
return 0;
}
std::string::find
是 C++ 标准库中 std::string
类的一个成员函数,用于查找指定子字符串在当前字符串中的位置。find
函数会返回该子字符串首次出现的位置(从 0 开始的索引)。std::string::npos
,这是一个特殊的常量,表示未找到。"world"
在字符串 "Hello, world!"
中的位置,并根据查找结果输出相应的信息。上一篇:c++ 开发
下一篇:c++ setprecision
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站