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类还支持 ...
随机推荐
- 【VUE】el-menu导航菜单过长、过多 超出窗口宽度 添加左右滚动按钮实现左右滚动效果
本文为原创文章,转载需注明出处~~ 效果图: 项目需求:如果一级菜单过多,需要出现滚动点击按钮. 准备工作:考虑到使用swiper插件,但swiper-slider必须是swiper-wrapper的 ...
- Asp .Net Core 系列:详解授权以及实现角色、策略、自定义三种授权和自定义响应
什么是授权(Authorization)? 在 ASP.NET Core 中,授权(Authorization)是控制对应用资源的访问的过程.它决定了哪些用户或用户组可以访问特定的资源或执行特定的操作 ...
- Codeforces Round 953 (Div. 2)
Codeforces Round 953 (Div. 2) 闲来无事水题解. A . B . C 显然 \(k\) 是偶数.考虑 \(k\) 的上界,\(p_{1}=n,p_{n}=1\),产生 \( ...
- 【Java】Map 映射接口 概述
Map 映射接口 概述 Map是一个双列数据,存储K-V类型的数据 JDK1.2 - HashMap 是目前Map的主要实现类 JDK1.2 线程不安全的,效率高,可存储null的key和value ...
- 【微信小程序】05 设备信息 & 请求API
获取系统信息(设备信息) https://developers.weixin.qq.com/miniprogram/dev/api/base/system/wx.getSystemInfo.html ...
- 【Android】虚拟设备运行BUG
虚拟设备是AndroidStudio提供的一个真机模拟运行环境 跑这个虚拟设备要下载手机系统镜像才能跑起来 然后项目中勾选这个虚拟设备,怎么设置就不赘述了 问题奇怪的是运行环境有了,App应用程序也能 ...
- C# Cefsharp 如何利用[Attribute]的把C#中的方法给到浏览器中调用
背景 "有没有遇见这样一个场景,需要注入到浏览器的类太多,又想统一管理且不遗漏,有没有什么好办法?""有有有,把头伸过来~" 解决办法 第一步:提供一个[Att ...
- shell脚本中$0 $1 $# $@ $* $? $$ 的各种符号意义详解
一.概述 shell中有两类字符:普通字符.元字符. 1. 普通字符 在Shell中除了本身的字面意思外没有其他特殊意义,即普通纯文本: 2. 元字符 是Shell的保留字符,在Shell中有着特殊的 ...
- Docker容器暴露ip至外网
参考地址:https://blog.csdn.net/lvshaorong/article/details/69950694
- Ynoi2016镜中的昆虫
[Ynoi 2016] 镜中的昆虫 简化题意 给定长为 \(n\) 序列 \(a\) , 两种操作 \(m\) 次: 1 l r x : 将 \([l , r]\) 修改为 \(x\) 2 l r : ...