#include <iostream>
#include <string>
int main() {
std::string str = "Hello, world!";
std::string substr = "world";
// 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 函数返回子字符串的起始位置(以字符索引表示);如果未找到,则返回 std::string::npos。"world" 在字符串 "Hello, world!" 中的位置,并根据查找结果输出相应的信息。上一篇:std在c++语言中
下一篇:c++ 虚函数
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站