string::npos的一些说明
一、定义
std:: string ::npos的定义:
static const size_t npos = -1;
表示 size_t 的最大值( Maximum value for size_t ) ,如果对 -1 表示size_t的最大值有疑问可以采用如下代码验证:
#include <iostream>
#include <limits>
#include <string>
using namespace std;
int main()
{
size_t npos = -1;
cout << "npos: " << npos << endl;
cout << "size_t max: " << numeric_limits<size_t>::max() << endl;
}
在我的PC上执行结果为:
npos: 4294967295
size_t max: 4294967295
可见他们是相等的,也就是说npos表示size_t的最大值
二、使用
2.1 如果作为一个 返回值 (return value) 表示没有找到匹配项 ,例如:
#include <iostream>
#include <limits>
#include <string>
using namespace std;
int main()
{
string filename = "test";
cout << "filename : " << filename << endl;
size_t idx = filename.find('.'); //作为return value,表示没有匹配项
if(idx == string::npos)
{
cout << "filename does not contain any period!" << endl;
}
}
2.2 但是string::npos作为string的成员函数的一个 长度参数 时,表示“ 直到字符串结束 (until the end of the string)”。例如:
tmpname.replace(idx+1, string::npos, suffix);
这里的string::npos就是一个长度参数,表示直到字符串的结束,配合idx+1表示,string的剩余部分。
#include <iostream>
#include <limits>
#include <string>
using namespace std;
int main()
{
string filename = "test.cpp";
cout << "filename : " << filename << endl;
size_t idx = filename.find('.'); //as a return value
if(idx == string::npos)
{
cout << "filename does not contain any period!" << endl;
}
else
{
string tmpname = filename;
tmpname.replace(idx + 1, string::npos, "xxx"); //string::npos作为长度参数,表示直到字符串结束
cout << "repalce: " << tmpname << endl;
}
}
执行结果为:
filename:test.cpp
replace: test.xxx
string::npos的一些说明的更多相关文章
- std::string::npos mean
std::string::npos 表示 no position, 没位置, 没找到
- string::npos,一个很大的数
string::npos,这是一个很大的数 npos 是这样定义的: static const size_type npos = -1; 因为 string::size_type (由字符串配置器 a ...
- C++中string.find()函数,string.find_first_of函数与string::npos
查找字符串a是否包含子串b,不是用strA.find(strB) > 0而是strA.find(strB) != string:nposstring::size_type pos = strA. ...
- std::string::find() 和 std::string::npos
npos是一个常数,用来表示不存在的位置,string::npos代表字符串到头了结束了. int idx = str.find("abc");if (idx == strin ...
- 字符串的查找删除---C++中string.find()函数与string::npos
给定一个短字符串(不含空格),再给定若干字符串,在这些字符串中删除所含有的短字符串 输入: 输入只有一组数据 输入一个短字符串(不含空格),再输入若干字符串直到文件结束为止 输出: 删除输入的短字符串 ...
- C++中string.find()函数与string::npos
先说说string::npos参数: npos 是一个常数,用来表示不存在的位置,类型一般是std::container_type::size_type 许多容器都提供这个东西.取值由实现决定,一般是 ...
- c++处理字符串string.find()与string::npos
1. string s = “xxx”; int a = s.find(‘x’); 如果没有匹配到,那么a = string::npos;
- C++中string::find()函数和string::npos函数的使用
1. string::find()函数和string::npos函数的介绍 我们在学习C++的时候必不可少的使用到string类中的find()函数,它是一个查找函数,功能还是很强大的,但是此处我们不 ...
- C++ string的查找函数和npos特殊值
STL中的string有6个查找函数: 1.find() 2.rfind() 从最后一个字符开始往前找. 3.find_first_of() 4.find_not_first_of() 5.find_ ...
随机推荐
- C:函数:功能:实现字符数组中所有字母的倒序存放并输出
前两天小测碰到一道题,建立一个函数,功能:实现字符数组中所有字母的倒序存放并输出,一开始觉得简单跟数字数组差不多,运行一下发现很多格式错误,这些是不必要的错误,现在就来说下,先说一下代码思路:定义一个 ...
- DNS查询相关
本文同时发表在https://github.com/zhangyachen/zhangyachen.github.io/issues/45 一种简单的设计方式是在因特网上使用一个DNS服务器,该服务器 ...
- lua 实现tableToString
function tableToString(studentNum) local str = "{ " str = str.."\n" for k, v in ...
- JVM虚拟机(一) 内存区域
JVM虚拟机内存组成: 如下图: 1. 程序计数器: (1)是一块较小的内存空间:可以看做当前程序执行子界面的行号指示器,字节码解析器执行的时候就是根据这个 ...
- Maven安装教程
一.安装Maven及配置环境变量 1.Maven官网地址:http://maven.apache.org/download.cgi 下载apache-maven-3.5.0-bin.zip文件 2. ...
- 房上的猫:java中的包
包 1.作用: (1)包允许将类组合成较小的单元(类似文件夹),易于找到和使用相应的类文件 (2)防止命名冲突: java中只有在不同包中的类才能重名 (3)包允许在更广的范围内保护类,数 ...
- Office 365也是.NET Core应用开发新战场
最近有幸阅读了陈希章花了一年时间为国内开发者贡献的<Office 365 开发入门指南>. 虽然早期接触过SharePoint的开发,2007年之后就再也没有接触SharePoint的开发 ...
- leetcode — copy-list-with-random-pointer
import java.util.*; /** * * Source : https://oj.leetcode.com/problems/copy-list-with-random-pointer/ ...
- gcc编译器用法
一个用c语言写的程序把他编译成计算机可执行的文件,一般有4个步骤 /*================================================================ ...
- 第十三章:Python の 网络编程进阶(二)
本課主題 SQLAlchemy - Core SQLAlchemy - ORM Paramiko 介紹和操作 上下文操作应用 初探堡垒机 SQLAlchemy - Core 连接 URL 通过 cre ...