string::npos的一些说明

时间:2022-09-08 13:25:47
一、定义 

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的一些说明的更多相关文章

  1. std&colon;&colon;string&colon;&colon;npos mean

    std::string::npos 表示 no position, 没位置, 没找到

  2. string&colon;&colon;npos,一个很大的数

    string::npos,这是一个很大的数 npos 是这样定义的: static const size_type npos = -1; 因为 string::size_type (由字符串配置器 a ...

  3. C&plus;&plus;中string&period;find&lpar;&rpar;函数&comma;string&period;find&lowbar;first&lowbar;of函数与string&colon;&colon;npos

    查找字符串a是否包含子串b,不是用strA.find(strB) > 0而是strA.find(strB) != string:nposstring::size_type pos = strA. ...

  4. std&colon;&colon;string&colon;&colon;find&lpar;&rpar; 和 std&colon;&colon;string&colon;&colon;npos

    npos是一个常数,用来表示不存在的位置,string::npos代表字符串到头了结束了.   int idx = str.find("abc");if (idx == strin ...

  5. 字符串的查找删除---C&plus;&plus;中string&period;find&lpar;&rpar;函数与string&colon;&colon;npos

    给定一个短字符串(不含空格),再给定若干字符串,在这些字符串中删除所含有的短字符串 输入: 输入只有一组数据 输入一个短字符串(不含空格),再输入若干字符串直到文件结束为止 输出: 删除输入的短字符串 ...

  6. C&plus;&plus;中string&period;find&lpar;&rpar;函数与string&colon;&colon;npos

    先说说string::npos参数: npos 是一个常数,用来表示不存在的位置,类型一般是std::container_type::size_type 许多容器都提供这个东西.取值由实现决定,一般是 ...

  7. c&plus;&plus;处理字符串string&period;find&lpar;&rpar;与string&colon;&colon;npos

    1. string s  = “xxx”; int a = s.find(‘x’); 如果没有匹配到,那么a = string::npos;

  8. C&plus;&plus;中string&colon;&colon;find&lpar;&rpar;函数和string&colon;&colon;npos函数的使用

    1. string::find()函数和string::npos函数的介绍 我们在学习C++的时候必不可少的使用到string类中的find()函数,它是一个查找函数,功能还是很强大的,但是此处我们不 ...

  9. C&plus;&plus; string的查找函数和npos特殊值

    STL中的string有6个查找函数: 1.find() 2.rfind() 从最后一个字符开始往前找. 3.find_first_of() 4.find_not_first_of() 5.find_ ...

随机推荐

  1. ssh&lpar;sturts2&lowbar;spring&lowbar;hibernate&rpar; 框架搭建之struts2

    一.struts2完整流程的逻辑(整体的概述) 首先,用户在地址栏中输入你的项目访问路径,然后这个请求会发送到服务器,之后服务器发现在web.xml中配置了一个filter过滤器,并且这个过滤器需要对 ...

  2. Spark on Mesos部署

    一.Mesos的安装和部署 1.下载mesos源码和依赖包 部署环境 centOS 6.6 mesos-0.21.0 spark-1.4.1 因为mesos官方只提供源码,所以必须要自己进行编译安装使 ...

  3. ssh登录很慢解决方法

    使用ssh客户端(如:putty)连接Linux服务器,可能会等待10-30秒才有提示输入密码.严重影响工作效率.登录很慢,登录上去后速度正常,这种情况主要有两种可能的原因: 1. DNS反向解析问题 ...

  4. shell第四篇(上)

    第四篇了解Shell 命令执行流程图 {网中人大哥推荐参考Learning the Bash Shell, 2nd Edition,第 178页:中文版229页} Shell 从标准输入或脚本中读取的 ...

  5. 双因素认证(2FA)教程

    所谓认证(authentication)就是确认用户的身份,是网站登录必不可少的步骤. 密码是最常见的认证方法,但是不安全,容易泄露和冒充. 越来越多的地方,要求启用 双因素认证(Two-factor ...

  6. spring mvc 的上传图片是怎么实现的?

    spring mvc 的上传图片是怎么实现的? 导入jar包,commons-io.jar 及 commons-fileupload.jar 在springmvc的配置文件中配置Mutipart解析器 ...

  7. csharp&colon;FTP Client Library using FtpWebRequest or Sockets

    https://netftp.codeplex.com/SourceControl/latest http://ftplib.codeplex.com/ https://www.codeproject ...

  8. javascript 两张图片切换 三目运算符

    <body> <script> function changeImage(){ var s = document.getElementById('myimage'); s.sr ...

  9. lintcode 二叉树后序遍历

    /** * Definition of TreeNode: * class TreeNode { * public: * int val; * TreeNode *left, *right; * Tr ...

  10. UVa 12235 状压DP Help Bubu

    题解戳这 一开始没看懂题解,后来想明白以后,d(i, j, s, x)是考虑第i本书的时候,前面已经拿走了j本书,剩下的书的种类的二进制状态为s,剩下的最后一本书的编号为x,所能得到的最小混乱度. 这 ...