一、关于npos的定义
在MSDN中有如下说明:
basic_string::npos
static const size_type npos = -1;//定义
The constant is the largest representable value of type size_type. It is assuredly larger than max_size(); hence it serves as either a very large value or as a special code.
以上的意思是npos是一个常数,表示size_t的最大值(Maximum value for size_t)。许多容器都提供这个东西,用来表示不存在的位置,类型一般是std::container_type::size_type。
#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;
}

执行结果为:

                 npos:           4294967295

                 size_t max:  4294967295

可见他们是相等的,也就是说npos表示size_t的最大值

二、npos的用法

1、npos可以表示string的结束位子,是string::type_size 类型的,也就是find()返回的类型。find函数在找不到指定值得情况下会返回string::npos。举例如下(计算字符串中含有的不同字符的个数):
#include <iostream>
#include <string>
using namespace std;
int main()
{string b;getline(cin,b);int count=0;for(int i=0;i<=127;i++)if(b.find(i)!=string::npos)count++;cout<<count;
}
举例2:
string name("Annaqijiashe");
int pos=name.find("Anna");
if(pos==string::npos)
cout<<"Anna not found!\n";
else  cout<<"Anna found at pos:"<<pos<<endl;
2、string::npos作为string的成员函数的一个长度参数时,表示“直到字符串结束(until the end of the string)”。例如:
  1. 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

三、值得注意的地方:

1、npos的类型
int idx = str.find("abc");
if (idx == string::npos)...
上述代码中,idx的类型被定义为int,这是错误的,即使定义为 unsigned int 也是错的,它必须定义为 string::size_type。因为 string::size_type (由字符串配置器 allocator 定义) 描述的是 size,故需为无符号整数型别。因为缺省配置器以型别 size_t 作为 size_type,于是 -1 被转换为无符号整数型别,npos 也就成了该型别的最大无符号值。不过实际数值还是取决于型别 size_type 的实际定义。不幸的是这些最大值都不相同。事实上,(unsigned long)-1 和 (unsigned short)-1 不同(前提是两者型别大小不同)。因此,比较式 idx == string::npos 中,如果 idx 的值为-1,由于 idx 和字符串string::npos 型别不同,比较结果可能得到 false。
要想判断 find() 的结果是否为npos,最好的办法是直接比较:

if (str.find("abc") == string::npos) { ... }

2、string 类提供了 6 种查找函数,每种函数以不同形式的 find 命名。这些操作全都返回 string::size_type 类型的值,以下标形式标记查找匹配所发生的位置;或者返回一个名为 string::npos 的特殊值,说明查找没有匹配。string 类将 npos 定义为保证大于任何有效下标的值。

参考资料:

string::npos 无符号数的陷阱

string::npos的一些说明

C++中string::npos的一些用法总结相关推荐

  1. C++中string::npos的用法总结

    C++中string::npos的用法总结 一.关于npos的定义 二.nops的常见用法 三.注意点 一.关于npos的定义 在cplusplus.com中有如下解释: static const s ...

  2. C++中string::npos

    一.关于npos的定义 npos是一个常数,表示size_t的最大值(Maximum value for size_t).许多容器都提供这个东西,用来表示不存在的位置,类型一般是std::contai ...

  3. java中String的七种用法

    转载:http://www.open-open.com/home/space.php?uid=2869&do=blog&id=8764 这一两天在学习string的一些用法,记得在来北 ...

  4. C++中 string::npos的含义

    string::npos参数 -- npos 是一个常数,用来表示不存在的位置 #include <bits/stdc++.h> using namespace std;int main( ...

  5. Js中String对象方法replace()用法详解

    replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串. 语法 stringObject.replace(regexp/substr,replacement) ...

  6. C++中string详解与用法

    string详解 string不是基本数据类型,是C++中对char封装成的类,所以C语言中没有string. String类是不可变的,对String类的任何改变,都是返回一个新的String类对象 ...

  7. Java中String的indexof()的用法

    目录 一.int indexOf(String str) :返回第一次出现的指定子字符串在此字符串中的索引 二.int indexOf(String str, int fromIndex): 从指定的 ...

  8. C#中string.format 格式转换用法详解

    String.Format 方法的几种定义: String.Format (String, Object) 将指定的 String 中的格式项替换为指定的 Object 实例的值的文本等效项. Str ...

  9. 关于C++中string头文件的用法

    注意: 这里需要声明一点,头文件string和string.h是不同的.下面的代码是string的使用例子,在string头文件下,函数中,是可以采取string str = "abcxyz ...

最新文章

  1. 孙正义:未来30年投资趋势【附PPT】
  2. vsftp 550,227 报错解决
  3. 汇编语言随笔(10)-内中断及实验12(返回到dos的中断处理程序)
  4. jdbc在项目中的应用
  5. c++ 获取计算机域名_每日一题| 计算机考研20200825
  6. 剑指Offer_61_序列化二叉树
  7. 以虚拟现实骨灰级开发者视角,来看VR智能硬件平台
  8. 简单理解梯度消失与梯度爆炸
  9. IIS 7.0 SSL 部署指南
  10. 组队APP功能点定点NABCD分析
  11. (day 1)创建项目--3【创建应用】
  12. Feed back TFS 2017 RC upgrade status to product team in product group 2017.03.01
  13. Git(2):如何更改GitHub仓库中项目的语言属性
  14. 简单的Dos攻击-死亡之Ping
  15. EXCEL工作表保护密码忘记,撤销保护攻略
  16. jetson xavier nx安装ROS Melodic
  17. 设计模式中的行为类模式
  18. java nio rewind_NIO-java.nio.ByteBuffer中flip、rewind、clear方法的区别
  19. 12个乒乓球,其中有11个球每个球重量一模一样,另外1个球重量和那11个球不一样.用天平称三次,把单独的球(和那11个重量不一样的球)找出来
  20. Leetcode-常数时间插入、删除、等概率获取一个数

热门文章

  1. C++ 斐波那契数列递归求解
  2. 「黑马程序员」微信小程序最新接口
  3. 对称加密算法之基于口令加密——PBE
  4. 实验报告:面包板搭建实际电路
  5. 你是不是不知道视频添加贴纸怎么弄
  6. hdu 2073 无限的路
  7. multi-task
  8. Kali linux安装pip2与pip3
  9. ai智能电话机器人如何
  10. 软件厂商6大忽悠伎俩揭秘