在 C++ 中,std::string::find() 是一个用于在字符串中查找子字符串或字符的成员函数。查找成功时返回匹配的索引位置,查找失败时返回 std::string::npos,表示未找到。

std::string::find() 函数原型

std::size_t find(const std::string& str, std::size_t pos = 0) const noexcept;
std::size_t find(const char* s, std::size_t pos = 0) const;
std::size_t find(char c, std::size_t pos = 0) const noexcept;

参数说明

  1. str/s:要查找的子字符串或字符。
  2. pos(可选):从哪个位置开始查找,默认为 0,即从字符串的开始位置查找。
  3. 返回值:查找成功时返回第一个匹配字符的索引,查找失败时返回 std::string::npos

std::string::npos

std::string::npos 是一个常量,表示查找操作失败或子字符串不存在时的返回值。具体定义为 std::string::npos = -1,它实际上是一个 std::size_t 类型的最大值。

示例

以下是一些简单的示例,演示如何使用 std::string::find()std::string::npos

查找子字符串

#include <iostream>
#include <string> int main() {
std::string str = "Hello, World!"; // 查找子字符串 "World"
std::size_t found = str.find("World");
if (found != std::string::npos) {
std::cout << "Found 'World' at index: " << found << std::endl; // 输出: Found 'World' at index: 7
} else {
std::cout << "'World' not found." << std::endl;
} // 查找不存在的子字符串 "Earth"
found = str.find("Earth");
if (found == std::string::npos) {
std::cout << "'Earth' not found." << std::endl; // 输出: 'Earth' not found.
} return 0;
}

查找字符

#include <iostream>
#include <string> int main() {
std::string str = "Hello, World!"; // 查找字符 'o'
std::size_t found = str.find('o');
if (found != std::string::npos) {
std::cout << "Found 'o' at index: " << found << std::endl; // 输出: Found 'o' at index: 4
} return 0;
}

从指定位置开始查找

#include <iostream>
#include <string> int main() {
std::string str = "Hello, World!"; // 从索引 5 开始查找字符 'o'
std::size_t found = str.find('o', 5);
if (found != std::string::npos) {
std::cout << "Found 'o' at index: " << found << std::endl; // 输出: Found 'o' at index: 8
} return 0;
}

总结

  • std::string::find():用于查找字符串或字符。返回子字符串第一次出现的索引,或者返回 std::string::npos 表示未找到。
  • std::string::npos:表示查找操作失败时的返回值(通常为最大值 -1 表示无效位置)。

string的find()与npos的更多相关文章

  1. C++ string的查找函数和npos特殊值

    STL中的string有6个查找函数: 1.find() 2.rfind() 从最后一个字符开始往前找. 3.find_first_of() 4.find_not_first_of() 5.find_ ...

  2. 标准C++中的string类的用法总结

    标准C++中的string类的用法总结 相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的确,MFC中的CString类使用起来真的非常的方便好用.但是如果离开了MFC框架,还有 ...

  3. c++:string函数

    string类的构造函数:string(const char *s);    //用c字符串s初始化string(int n,char c);     //用n个字符c初始化此外,string类还支持 ...

  4. string find

    string类的查找函数: ) const;//从pos开始查找字符c在当前字符串的位置 ) const;//从pos开始查找字符串s在当前串中的位置 int find(const char *s, ...

  5. C++中string,wstring,CString的基本概念和用法

    一.概念 string和CString均是字符串模板类,string为标准模板类(STL)定义的字符串类,已经纳入C++标准之中.wstring是操作宽字符串的类.C++标准程序库对于string的设 ...

  6. C++ STL string

    要想使用标准C++中string类,必须要包含 #include <string>// 注意是<string>,不是<string.h>,带.h的是C语言中的头文件 ...

  7. C++string中有关字符串内容修改和替换的函数浅析

    1.assign() 原型: //string (1) basic_string& assign (const basic_string& str); //substring (2) ...

  8. VC++ 标准C++中的string类的用法总结

    相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的确,MFC中的CString类使用起来真的非常的方便好用.但是如果离开了MFC框架,还有没有这样使用起来非常方便的类呢?答案是肯 ...

  9. [C++][语言语法]标准C++中的string类的用法总结

    转自:http://www.cnblogs.com/xFreedom/archive/2011/05/16/2048037.html 要想使用标准C++中string类,必须要包含 #include ...

  10. C++中string 的使用

    string类的构造函数:string(const char *s);    //用c字符串s初始化string(int n,char c);     //用n个字符c初始化此外,string类还支持 ...

随机推荐

  1. 从菜鸟到大牛!嵌入式完整学习路线:STM32单片机-RTOS-Linux(文末领取开发板全套资料)

    ​ 嵌入式系统是许多现代电子设备和智能系统的核心,掌握嵌入式系统,意味着能够设计和开发更加智能化的产品.本文为所有想进入嵌入式领域的初学者提供一个完整系统学习的路线图,按照 "STM32单片 ...

  2. httpclient,轻量级idea集成测试工具

    优点:不用新开一个网页,具有测试数据保存功能,不需要配置即用(对比swagger)     不会特别占内存(对比postman) 使用方法:idea中安装插件 controller方法中点击 选择对应 ...

  3. NPIO在指定位置插入新列(附案例和代码)

    背景: I could be mistaken as I am not that familiar with NPOI, however, after a minor search, it appea ...

  4. win10安装和使用wireshark

    win10安装和使用wiresharkhttps://blog.csdn.net/qq_34732729/article/details/105126146https://blog.csdn.net/ ...

  5. 【SQL】 牛客网SQL训练Part3 较难难度

    获取当前薪水第二多的员工的emp_no以及其对应的薪水salary 请你查找薪水排名第二多的员工编号emp_no.薪水salary.last_name以及first_name,不能使用order by ...

  6. NVIDIA的Isaac AMR产品介绍

    NVIDIA的Isaac AMR是仓库自动运货机器人项目,说直白些就是一个AGV的小车,不过和传统的AGV不同,NVIDIA推出的这个产品是智能化的.传统AGV小车的运行代码都是写死的,直接把运行命令 ...

  7. 支持NVIDIA GPU —— 如何运行docker环境下的Jax环境

    项目地址: https://github.com/NVIDIA/JAX-Toolbox 具体的安装命令: 地址: https://github.com/NVIDIA/JAX-Toolbox/pkgs/ ...

  8. MindSpore计算框架如何发布训练好的模型到官方模型仓库MindSpore_Hub上

    相关官方资料: https://www.mindspore.cn/tutorial/training/zh-CN/r1.2/use/publish_model.html 参考地址: https://g ...

  9. 最短小精悍的js数组打乱顺序

          let number = [1, 45, 13, 17];       // 封装一个打乱数组的方法       function getarr(arr) {         return ...

  10. kubernetes中集成istio出现拉取配置中心数据失败导致服务启动失败 荐

    由于在k8s使用了grpc,所以这里我们集成istio来实现http2的自动发现以及负载均衡,但是随着节点增加,istio之前同步配置时间边长导致第一次启动时,服务启动拉取配置时istio却还没初始化 ...