std::string::npos
来源:互联网 发布:禁止安装某些软件 编辑:程序博客网 时间:2024/05/13 04:49
int idx = str.find("abc");
if (idx == string::npos)
...
上述代码中,idx的类型被定义为int,这是错误的,即使定义为 unsigned int 也是错的,它必须定义为 string::size_type。
npos 是这样定义的:
static const size_type npos = -1;
因为 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) { ... }
(EOF)
转自:http://blog.sina.com.cn/s/blog_49370c500100ov3k.html
- std::string::npos
- std::string::npos
- std::string::npos
- std::string::find() 和 std::string::npos
- std::string::find() 和 std::string::npos
- std::string::find() 和 std::string::npos
- std::string::find() 和 std::string::npos
- std::string::find() 和 std::string::npos
- std::string::find() 和 std::string::npos
- std::string::find() 和 std::string::npos
- std::string::find() 和 std::string::npos
- td::string::find() 和 std::string::npos
- (地基工)std::string::find() 和 std::string::npos
- std::string::find 空字符串 返回结果不是 string::npos
- c++文件操作std::ifstream std::string::find() 和 std::string::npos
- string::npos
- string::npos
- string::npos
- 内存对齐的规则以及作用
- Raspberry pi 使用python+pySerial实现串口通信
- 一个硬盘和内存的感人爱情故事!墙裂推荐!!!
- 大话设计模式之原型模式中的复制
- 改进的单源最短路径(Graphlab)
- std::string::npos
- 幽门螺旋杆菌
- ActiveX控件制作与发布,如何将您的C++程序嵌入到浏览器中
- C++中的位拷贝和值拷贝
- Table 组件使用指南:使用Poll组件刷新Table
- 2013年3月6日学习日记:完成Samba服务器搭建
- 在CentOS 6.3 下安装 ipvsadm 1.26.1 & keepalived 1.2.5
- 如何将字典Dictionary数据绑定到DataGridView控件上
- 利用队列遍历用儿子/兄弟链存储的树