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_ ...
随机推荐
- boost::assign(标准容器填充库)
boost::assign通过对"+="和","的重载非常方便的填充标准容器(std::vector,std::set,std::list,std::map), ...
- css3 结构性伪类选择器
伪类 选择器 类型 说明 备注 E:first-line 伪元素选择器 选择匹配E元素内的第一行文本 E:first-letter 伪元素选择器 选择匹配E元素内的第一个字符 E:before 伪元素 ...
- 有关opacity或RGBA设置颜色值及元素的透明值
opacity声明来设置元素的透明值,当opacity设置元素的透明值,内部的文字及元素也会透明,通过RGBA设置的颜色值只针对当前元素,内部的文字及元素的透明值并未发生变化 opacity声明来 ...
- BCL和CoreFx的区别
bcl是.netframework clr 的基础库corefx是.net core clr的基础库
- vue-router源码学习(一)
因为v3.01版本中的 /src代码使用TypeScript进行书写,我这里仅仅用作模块学习, 具体学习的还是 /dist/vue-router.js 代码. (一)基本使用方式 JS代码 // ...
- Django学习日记03_模型_Fields
创建模型 模型对应工程中的应用,一个工程可能包含很多的应用,通过命令 python manage.py startapp myapp 创建一个叫myapp的应用,django会帮助生成以下目录: po ...
- Oracle绑定变量优缺点
参考:http://f.dataguru.cn/thread-208881-1-1.html 参考:http://blog.sina.com.cn/s/blog_4d9ece9a0100caw8.ht ...
- vmware一步步安装centos
软件环境:vmware10.0破解版 centos版本:6.4 1.启动vmware,新建虚拟机,选择自定义安装 2,出现如下界面,保持默认,点击下一步 3.这个步骤要特别注意,选择“稍后安装”,我们 ...
- JS中typeof和instanceof用法区别
typeof和instanceof都可以用来判断变量 1.typeof用以获取一个变量或者表达式的类型,typeof一般只能返回如下几个结果: number,boolean,string,functi ...
- 中文版microbit:TurnipBit显示动态滚动字符教程实例
随着当今社会的发展,社会的进步,家长们越来越忙碌,致使家长们在孩子成长过程中陪孩子的互动的时间越来越少,为此,TurnipSmart公司制作的一款MicroPython开发板--TurnipBit,这 ...