Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

c++ string find

作者:夏威夷丶霪男   发布日期:2026-04-29   浏览:51

#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++ 虚函数

大家都在看

c++闭包

c++向上取整的代码

c++单引号和双引号的区别

c++ 注释

c++如何判断素数

c++怎么输入字符串

c++ functional

c++框架代码

c++格式化字符串

c++ orm框架

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站