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类还支持 ...
随机推荐
- 开源照片管理神器 PhotoPrism 安装和使用教程
如今我们每个人都积累了海量的照片和视频,做自媒体的 UP 主们积累的照片和视频数量可能更多.面对这么多的照片和视频,我们该如何管理呢? 之前我一直用谷歌相册,因为它有很多优势,比如无限空间,支持智能整 ...
- oauth2协议
什么是OAUTH2协议: 首先是几个概念问题: 资源:用户信息,在微信中存储 资源拥有者:用户 认证服务:微信负责认证用户的身份,也负责为客户端颁发令牌 客户端:携带令牌请求微信获取用户信息 仍以微信 ...
- windows生成苹果私钥证书p12证书和profile文件的方法
hbuilderx出现已经有差不多10年时间了,现在越来越多的企业,开始使用跨平台性更优秀的uniapp来开发ios app. 开发ios app的时候,打包需要苹果的私钥证书和证书profile文件 ...
- vue3 + ts 中出现 类型“typeof import(".........../node_modules/vue/dist/vue")”的参数不能赋给类型“Component<any, any, any, ComputedOptions, MethodOptions>”的参数。
错误示例截图 解决方法 修改shims-vue.d.ts中的内容 declare module "*.vue" { import { defineComponent } from ...
- 【Java】Collection子接口:其二 Set 组接口
Collection子接口:其二 Set 组接口 - Set接口是Collection的子接口,Set没有提供额外的方法 - Set集合中不允许包含重复的元素,如果重复添加,只保留最新添加的那一个 - ...
- 3天搞定Linux,1天搞定Shell笔记
Linux概述 Linux是一个操作系统OS 开源 MacOS基于Darwin,Darwin基于FreeBSD开发. Linux基于Minix(开发重写),Minix基于Unix开发. Linux一切 ...
- 【转载】 NCCL(Nvidia Collective multi-GPU Communication Library) Nvidia英伟达的Multi-GPU多卡通信框架NCCL 学习;PCIe 速率调研
原文地址: https://www.cnblogs.com/xuyaowen/p/nccl-learning.html ---------------------------------------- ...
- Hessian Free Optimization——外国网友分享的“共轭梯度”的推导
外国网友分享的"共轭梯度"的推导: https://andrew.gibiansky.com/blog/machine-learning/hessian-free-optimiza ...
- baselines算法库run.py模块分析
baselines算法库地址: https://gitee.com/devilmaycry812839668/baselines =================================== ...
- 免费word简历 简历制作平台
分享一个简历制作平台. 免费的word模版 链接地址 https://www.xyjianli.com/ https://www.xyjianli.com/list https://www.xyjia ...