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_ ...
随机推荐
- HTML列表的常用属性及其应用
首先列表分成有序和无序分别是<ol><ul>,无序的比较简单,看个例子: <html> <body> <h4>一个无序列表:</h4& ...
- nova创建虚拟机源码系列分析之二 wsgi模型
openstack nova启动时首先通过命令行或者dashborad填写创建信息,然后通过restful api的方式调用openstack服务去创建虚拟机.数据信息从客户端到达openstack服 ...
- ubuntu12.04destdrop删除不必要的软件
sudo apt-get -y --auto-remove purge unity unity-2d* sudo apt-get -y purge empathy sudo apt-get -y ...
- 刚实习的自己-php
刚毕业的大学生,可能你的理论知识很丰富,但是你要清楚的是:你缺少实战经验. 正式实习的时候是在下午,老板给了我一个他们几年前开发好的系统(cms),这是一个展示型的网站,也就是发 ...
- jmeter远程分布执行遇到的网卡坑(A Test is currently running,stop or ....)
周末加班做一个项目app的性能测试,单机负载时由于公司给每个人的网络带宽上传下载流量就1M,300个用户并发就已经网络IO饱和了,虽然和相关部门协调过资源问题,但是收效甚微,因此打算先用分布部署压力机 ...
- java之自动过滤提交文本中的html代码script代码
public class test { public static String Html2Text(String inputString) { String htmlStr = inputStrin ...
- uEditor富文本编辑器
在配合vue和webpack使用时,在严格模式下注意会报错,需要修改配置. 我采用的方式:----不建议全部取消严格模式,因为一些插件必须要再严格模式才能运行的 对UEditor的严格模式报错文件,取 ...
- shell日志删除(超容量&自动)
背景:避免双十一磁盘被打爆,本想通过crontab执行,但是删除需要密码,所以用作当机器磁盘高于摸个阈值,进行无关性日志强删 #!/bin/sh #use #sh clean.sh wmporder_ ...
- > library('ggplot2') Error in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]) : 不存在叫‘colorspace’这个名字的程辑包
> library('ggplot2')Error in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]) : ...
- python 模块:csv
转自:http://www.cnblogs.com/sislcb/archive/2008/12/15/1355481.html (感谢整理) 举几个例子来介绍一下,Python 的 CSV模块的使用 ...