在 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. java面试一日一题:mysql执行delete数据真的被删除了吗

    问题:请讲下mysql执行了delete操作,数据真的被删除了吗 分析:这个问题考察对mysql底层存储的理解. 回答要点: 主要从以下几点去考虑, 1.肯定没有真正删除? 2.为什么这样设计? my ...

  2. 【SpringMVC】06 转发 & 重定向

    除了快速入门的视图解析器方式处理, 我们还可以使用原生的Servlet转发方式执行 访问测试 还有重定向 访问 测试 使用SpringMVC的转发&重定向 和原生的重定向,有一点不同,MVC的 ...

  3. python3解析wav文件获取dtmf值

    操作系统 :Windows 10_x64 Python版本:3.9.2 从事FreeSwitch相关工作,大概率会遇得到DTMF,DTMF的传递方式有三种: In-band RFC2833 SIP-I ...

  4. NVIDIA显卡如何进一步压榨性能 —— 开启单用户独享模式

    开启单用户独享模式可以提高显卡利用率,但是最大的缺点就是开启后显卡中只能有一个用户的程序,其他用户的程序只能等待显卡中原有程序全部退出才可以使用显卡,因此该种模式只适合于个人电脑,不适合于服务器(没有 ...

  5. 国产AI训练卡,对标美国NVIDIA公司的A100,华为昇腾Atlas 300T A2(Ascend 910B4)高性能GPU/NPU/AI推理/国产计算/信创训练卡 —— 电商平台已开售

    China has successfully achieved the localization of AI chips, breaking through the technological res ...

  6. Linux的netns使用总结

    转载请注明出处: Linux的netns(Network Namespace)是Linux内核提供的一项强大的网络隔离功能,它能够创建多个独立的网络空间,每个空间都拥有自己独立的网络协议栈,包括网络接 ...

  7. 如何在AWS上构建Apache DolphinScheduler

    引言 随着云计算技术的发展,Amazon Web Services (AWS) 作为一个开放的平台,一直在帮助开发者更好的在云上构建和使用开源软件,同时也与开源社区紧密合作,推动开源项目的发展. 本文 ...

  8. WPF 实现图标按钮

    假设需要实现一个图标和文本结合的按钮 ,普通做法是 直接重写该按钮的模板: 如果想作为通用的呢? 两种做法: 附加属性 自定义控件 推荐使用附加属性的形式 第一种:附加属性 创建Button的附加属性 ...

  9. 每天那么多工作,我为什么能做到 "不忘事" ?

    大家好,我是程序员鱼皮. 我相信很多朋友都遇到过丢失工作.或者忘记事情的情况,尤其是事情一多,就更容易遗漏:而如果在工作中你漏掉了某项任务,需要上级或同事重复提醒你,是很影响别人对你的印象的. 那么如 ...

  10. 使用 nuxi build-module 命令构建 Nuxt 模块

    title: 使用 nuxi build-module 命令构建 Nuxt 模块 date: 2024/8/31 updated: 2024/8/31 author: cmdragon excerpt ...