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类还支持 ...
随机推荐
- Spectre.Console.Cli注入服务的几种姿势
Spectre.Console大家可能都不陌生,写控制台程序美化还是不错的,支持着色,表格,图标等相当nice,如果对这个库不熟悉我强烈推荐你了解一下,对于写一些CLI小工具还是相当方便的, 本文主要 ...
- 搭建lnmp环境-nginx关联php-fpm (第三步)
永久关闭防火墙sudo systemctl stop firewalldsudo systemctl disable firewall 有两个防火墙!如果上面那个关闭还不行,就继续关这个后重启. ...
- WPF MVVM模式简介
WPF是Windows Presentation Foundation的缩写,它是一种用于创建桌面应用程序的用户界面框架.WPF支持多种开发模式,其中一种叫做MVVM(Model-View-ViewM ...
- 7月22号python 每日一题
7月22号python 每日一题 LCR 121. 寻找目标值 - 二维数组 难度:中等 m*n 的二维数组 plants 记录了园林景观的植物排布情况,具有以下特性: 每行中,每棵植物的右侧相邻植物 ...
- 5、Git之版本号
5.1.概述 每一次提交,Git 都会生成相关的版本号:每个版本号由 40 位 16 进制的数字组成. 这 40 位 16 进制的数字,是根据提交的内容,通过 SHA-1 算法计算出来的. 版本号具体 ...
- window系统多用户登录软件——ASTER——终端共享器——网络终端机
Windows系统除了Server版其他的都是单用户系统,但是其实Windows系统都是内置支持多用户的,只不过除了Server版本以外的系统版本都是将多用户登录功能屏蔽掉了,如果我们可以解除掉win ...
- Visual Studio 个人配置和插件
主题和字体 一般为黑色深色主题,看起来比较舒服. 字体使用Fira Code,好处就是它把 =>和!=换成更加熟悉的表示.就比如以下.缺点就是习惯之后,看别人的代码就不习惯. 插件 当然是首推R ...
- Git报错解决:OpenSSL SSL_read: Connection was reset, errno 10054 错误解决
1.背景 最近因项目上需要,想借鉴一下spring里面的一下架构设计,在拉取spring源码是报错如下: 初步判定,估计是访问国外的网站,网速受限的原因..... 2.解决方案 打开Git命令页面,执 ...
- DataOps真能“降本增效”?
在各行各业中,越来越多的公司开始重视收集数据,并寻找创新方法来获得真实可行的商业成果,并且愿意投入大量时间和金钱来实现这一目标. 据IDC称,数据和分析软件及云服务市场规模在 2021 年达到了 90 ...
- macOS 移除顽固打印机信息
问题描述 当我打开 Parallels Desktop 的 Ubuntu 虚拟机时,总是会看到打印机已添加的提示: 查看已有打印机信息 $ lpstat -p 打印机Lenovo_M7206W闲置,启 ...