string的find()与npos
在 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;
参数说明
str/s:要查找的子字符串或字符。pos(可选):从哪个位置开始查找,默认为0,即从字符串的开始位置查找。- 返回值:查找成功时返回第一个匹配字符的索引,查找失败时返回
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的更多相关文章
- C++ string的查找函数和npos特殊值
STL中的string有6个查找函数: 1.find() 2.rfind() 从最后一个字符开始往前找. 3.find_first_of() 4.find_not_first_of() 5.find_ ...
- 标准C++中的string类的用法总结
标准C++中的string类的用法总结 相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的确,MFC中的CString类使用起来真的非常的方便好用.但是如果离开了MFC框架,还有 ...
- c++:string函数
string类的构造函数:string(const char *s); //用c字符串s初始化string(int n,char c); //用n个字符c初始化此外,string类还支持 ...
- string find
string类的查找函数: ) const;//从pos开始查找字符c在当前字符串的位置 ) const;//从pos开始查找字符串s在当前串中的位置 int find(const char *s, ...
- C++中string,wstring,CString的基本概念和用法
一.概念 string和CString均是字符串模板类,string为标准模板类(STL)定义的字符串类,已经纳入C++标准之中.wstring是操作宽字符串的类.C++标准程序库对于string的设 ...
- C++ STL string
要想使用标准C++中string类,必须要包含 #include <string>// 注意是<string>,不是<string.h>,带.h的是C语言中的头文件 ...
- C++string中有关字符串内容修改和替换的函数浅析
1.assign() 原型: //string (1) basic_string& assign (const basic_string& str); //substring (2) ...
- VC++ 标准C++中的string类的用法总结
相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的确,MFC中的CString类使用起来真的非常的方便好用.但是如果离开了MFC框架,还有没有这样使用起来非常方便的类呢?答案是肯 ...
- [C++][语言语法]标准C++中的string类的用法总结
转自:http://www.cnblogs.com/xFreedom/archive/2011/05/16/2048037.html 要想使用标准C++中string类,必须要包含 #include ...
- C++中string 的使用
string类的构造函数:string(const char *s); //用c字符串s初始化string(int n,char c); //用n个字符c初始化此外,string类还支持 ...
随机推荐
- 写写Redis十大类型zset的常用命令
其实这些命令官方上都有,而且可读性很强,还有汉化组翻译的http://redis.cn/commands.html,不过光是练习还是容易忘,写一写博客记录一下 从zset类型开始写||zset类型适合 ...
- 5、Git之版本号
5.1.概述 每一次提交,Git 都会生成相关的版本号:每个版本号由 40 位 16 进制的数字组成. 这 40 位 16 进制的数字,是根据提交的内容,通过 SHA-1 算法计算出来的. 版本号具体 ...
- 【Mybatis】14 缓存
1.什么是缓存? - 缓存是指把经常需要读写的数据,保存到一个高速的缓冲区中,这个行为叫缓存 - 也可以是指被保存在高速缓冲区的数据,也叫缓存 2.Mybatis缓存 Mybatis中分为一级缓存和二 ...
- 【Vue】Re03 computed属性计算和ES6的一些补充
一.Computed属性计算 四种计算处理的方式方法 <!DOCTYPE html> <html lang="en"> <head> <m ...
- 读论文《基于 GA - BP 的汽车行李箱盖内板冲压成形工艺优化》 —— 如何使用AI技术优化模具产业中工件冲压工艺
最近到了模具公司工作,本来以为身边同事对模具生产和工件生产的流程(大致流程)会比较了解,结果一问才知道基本都是一问三不知,大家都在模具公司工作但是貌似很多人干的和模具生产和工件制造的工作关联性并不强, ...
- git警告信息:Encountered 1 file(s) that may not have been copied correctly on Windows: —— See: `git lfs help smudge` for more details.
git报警信息: 官方讨论的帖子: https://github.com/git-lfs/git-lfs/issues/2434 说下个人的理解: 在git管理中,对于大文件(一般为压缩后的二进制文件 ...
- 如何在CMAKE中指定python路径——使用cmake为python编译扩展模块时指定python路径
答案: cmake -DPython3_EXECUTABLE=/path/to/bin/python3 ================================================ ...
- 【转载】 Mobaxterm 中文输入Backspace按键问题
版权声明:本文为CSDN博主「Flynnsin」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明.原文链接:https://blog.csdn.net/qq_45830 ...
- 如何将 Vim 剪贴板里面的东西粘贴到 Vim 之外的地方? (Ubuntu18.04系统亲测)
主要参考内容: https://www.zhihu.com/question/19863631 在vim中剪贴中的内容是难以在vim之外使用的,那么怎么修改这个问题呢? =============== ...
- [天线原理及设计>基本原理] 1. 辐射机制
1. 辐射机制 1.1. Single Wire 单线 如果电荷不移动,则不会产生电流,也不会产生辐射. 如果电荷以匀速移动: a. 如果电线是直的,并且范围是无限的,则没有辐射. b. 如果电线弯曲 ...