std::string find 的返回值
std::string 的方法 find,返回值类型是std::string::size_type, 对应的是查找对象在字符串中的位置(从0开始),
如果未查找到,该返回值是一个很大的数据(4294967295),判断时与 std::string::npos 进行对比
std::string str("abcdefg");
std::string::size_type pos = str.find("abc");
if (pos != std::string::npos)
{
cout << "Not find" << endl;
}
或
std::string str("abcdefg");
if (str.find("abc") != std::string::npos)
{
cout << "Not find" << endl;
}
很多同学由于经常使用 CString 的缘故,喜欢这样写
std::string str("abcdefg");
int pos = str.find("abc");
if (pos < )
{
cout << "Not find" << endl;
}
这样写理论上也是可以的,因为 size_type 相当于 unsigned int类型,最大值4294967295强制转换为int型,就是-1
但下面的写法是错误的:
std::string str("abcdefg");
if (str.find("abc") < ) //错误,应该写成 != std::string::npos
{
cout << "Not find" << endl;
}
最后,建议使用size_type,这样可以适应不同的平台。因为int 类型的大小会根据不同平台而不同。 拓展:
s.find(s2) 第一次出现的位置
s.rfind 最后一次出现的位置
s.find_first_of(s2) 任何一个字符第一次出现的位置
s.find_last_of 任何一个字符最后一次出现的位置
s.find_first_not_of(s2) 第一个不在s2中的字符所在位置
s.find_last_not_of(s2) 最后一个不在s2中的字符所在位置
《完》
std::string find 的返回值的更多相关文章
- java.lang.String类compareTo()返回值解析
一.compareTo()的返回值是int,它是先比较对应字符的大小(ASCII码顺序)1.如果字符串相等返回值02.如果第一个字符和参数的第一个字符不等,结束比较,返回他们之间的差值(ascii码值 ...
- HibernateTemplate的find(String querystring)返回值具体解释
项目源代码中出现例如以下代码: HibernateTemplate ht =-- List<Object[]> tempList = ht.find(String querystring) ...
- LiteQuery MAX(Integer)、MAX(String) 判断是否返回值
unit Unit6; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...
- 一段小代码秒懂C++右值引用和RVO(返回值优化)的误区
关于C++右值引用的参考文档里面有明确提到,右值引用可以延长临时变量的周期.如: std::string&& r3 = s1 + s1; // okay: rvalue referen ...
- ajaxpro返回值类型总结-DataTable(转)
ajaxpro使用总结系列其他内容 ajaxpro ajaxmethod 重载调用问题 ajaxpro方法ajaxmethod调用示例 ajaxpro返回值类型总结-string,int ajaxpr ...
- SSM-SpringMVC-20:SpringMVC中处理器方法之返回值void篇
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 处理器的方法我们之前做过,返回值为String的,返回值为ModelAndView的,我们这个讲的这个返回 ...
- SpringMVC方法的返回值类型和自动装配
1. void类型作为返回值类型 /** * 如果方法写成了void就跟原来servlet含义是差不多 的 * json */ @RequestMapping("/firstRequest& ...
- SpringMVC——返回值类型
1.void作为返回值类型 如果你的方法写成了Void就跟原来Servlet含义是差不多的 @RequestMapping("/index*") public void first ...
- std::string 和 CString问题
std::string stdTemp; CString strTemp; strTemp = stdTemp; ;//这一步直接赋值可不可以 因为CString可以接受const char*的 ...
随机推荐
- strpos检测字符串是否包含元素
<?php echo strpos('https://www.baidu.com','https',0); ?> 默认的第三个参数就是0. 第三个参数表示查找的起始位置. 如果不存在的话, ...
- [转]VS中的路径宏 OutDir、ProjectDir、SolutionDir各种路径含义
转自 http://blog.csdn.net/restraint/article/details/39934401 说明 $(RemoteMachine) 设置为“调试”属性页上“远程计算机”属性的 ...
- java——File
注意事项: 1:创建File对象需要导包, import java.io.File 2:File对象没有无参数构造.创建对象需要传参. 3:File类的对象,既可以代表文件也可以代表文件夹. 构造 ...
- 关于VUE调用父实例($parent) 根实例 中的数据和方法
this.$parent或者 this.$root 在子组件中判断this.$parent获取的实例是不是父组件的实例 在子组件中console.log(this.$parent) 在父组件中con ...
- 《剑指offer》第六十七题(把字符串转换成整数)
// 面试题67:把字符串转换成整数 // 题目:请你写一个函数StrToInt,实现把字符串转换成整数这个功能.当然,不 // 能使用atoi或者其他类似的库函数. #include <ios ...
- 不要在Lua中使用os.clock()函数
1.os.clock函数的实现是调用了c语言的函数函数库,实现代码如下: static int os_clock (lua_State *L) { lua_pushnumber(L, ((lua_Nu ...
- JAVA基础知识总结:十七
一.转换流 作用:实现将字节流转换为字符流 a.InputStreamReader:字节字符转换输入流:字节输入流----->字符输入流 b.OutputStreamWriter:字节字符转换输 ...
- Introduction to dnorm, pnorm, qnorm, and rnorm for new biostatisticians
原文:Introduction todnorm,pnorm,qnorm, andrnormfor new biostatisticians Today I was in Dan’s office ho ...
- English trip V1 - B 13. Are you a model? 你是模特吗? Teacher:Patrick Key: 单词回顾、词性后缀
因为这节课本身内容过于So easy~ Patrick给我补充了很多课外内容 课上内容(Lesson) I doesn't work 2层意思 1) 东西坏了,这个东西不能正常工作了.比如钟不走时间 ...
- OnSen UI结合AngularJs打造”美团"APP"逛一逛”页面 --Hybrid App
1.页面效果图: 演示链接地址:http://www.nxl123.cn/bokeyuan/meiTuanDemo_walk/ 2.核心代码 walk.html: <ons-page id=&q ...