C++中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.
#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的用法
#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;
}
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;
- 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
三、值得注意的地方:
int idx = str.find("abc");
if (idx == string::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的一些用法总结相关推荐
- C++中string::npos的用法总结
C++中string::npos的用法总结 一.关于npos的定义 二.nops的常见用法 三.注意点 一.关于npos的定义 在cplusplus.com中有如下解释: static const s ...
- C++中string::npos
一.关于npos的定义 npos是一个常数,表示size_t的最大值(Maximum value for size_t).许多容器都提供这个东西,用来表示不存在的位置,类型一般是std::contai ...
- java中String的七种用法
转载:http://www.open-open.com/home/space.php?uid=2869&do=blog&id=8764 这一两天在学习string的一些用法,记得在来北 ...
- C++中 string::npos的含义
string::npos参数 -- npos 是一个常数,用来表示不存在的位置 #include <bits/stdc++.h> using namespace std;int main( ...
- Js中String对象方法replace()用法详解
replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串. 语法 stringObject.replace(regexp/substr,replacement) ...
- C++中string详解与用法
string详解 string不是基本数据类型,是C++中对char封装成的类,所以C语言中没有string. String类是不可变的,对String类的任何改变,都是返回一个新的String类对象 ...
- Java中String的indexof()的用法
目录 一.int indexOf(String str) :返回第一次出现的指定子字符串在此字符串中的索引 二.int indexOf(String str, int fromIndex): 从指定的 ...
- C#中string.format 格式转换用法详解
String.Format 方法的几种定义: String.Format (String, Object) 将指定的 String 中的格式项替换为指定的 Object 实例的值的文本等效项. Str ...
- 关于C++中string头文件的用法
注意: 这里需要声明一点,头文件string和string.h是不同的.下面的代码是string的使用例子,在string头文件下,函数中,是可以采取string str = "abcxyz ...
最新文章
- 孙正义:未来30年投资趋势【附PPT】
- vsftp 550,227 报错解决
- 汇编语言随笔(10)-内中断及实验12(返回到dos的中断处理程序)
- jdbc在项目中的应用
- c++ 获取计算机域名_每日一题| 计算机考研20200825
- 剑指Offer_61_序列化二叉树
- 以虚拟现实骨灰级开发者视角,来看VR智能硬件平台
- 简单理解梯度消失与梯度爆炸
- IIS 7.0 SSL 部署指南
- 组队APP功能点定点NABCD分析
- (day 1)创建项目--3【创建应用】
- Feed back TFS 2017 RC upgrade status to product team in product group 2017.03.01
- Git(2):如何更改GitHub仓库中项目的语言属性
- 简单的Dos攻击-死亡之Ping
- EXCEL工作表保护密码忘记,撤销保护攻略
- jetson xavier nx安装ROS Melodic
- 设计模式中的行为类模式
- java nio rewind_NIO-java.nio.ByteBuffer中flip、rewind、clear方法的区别
- 12个乒乓球,其中有11个球每个球重量一模一样,另外1个球重量和那11个球不一样.用天平称三次,把单独的球(和那11个重量不一样的球)找出来
- Leetcode-常数时间插入、删除、等概率获取一个数