string.find() 在使用 ==-1 时返回 true,但在使用 <0 时返回 false

string.find() returns true when ==-1 is used but false when lt;0 is used(string.find() 在使用 ==-1 时返回 true,但在使用 lt;0 时返回 false)
本文介绍了string.find() 在使用 ==-1 时返回 true,但在使用 <0 时返回 false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在字符串中查找一个字符,但我得到了意想不到的结果.我的理解是 string::find(char c) 在找不到时返回 -1 .但是,我得到了一些意想不到的结果.

即使字符串不包含 '8',它仍然返回 true.

std::string s = "123456799";if(s.find('8')<0)cout <<未找到"<<结束;别的cout <<找到"<<结束;//输出:找到

但是,当使用 == 时,代码会按预期工作.

std::string s = "123456799";if(s.find('8')==-1)cout <<未找到"<<结束;别的cout <<找到"<<结束;//输出:未找到

解决方案

我的理解是string::find(char c)在找不到的时候返回-1.

这不准确.根据文档:

<块引用>

返回值
找到的子字符串的第一个字符的位置,如果没有,则为 npos找到了这样的子字符串.

准确地说,当找不到 std::string::find 时将返回 std::string::npos.重点是std::string::npos的类型是std::string::size_type,是无符号整数类型.即使它是从 -1 的值初始化的,它也不是 -1;它仍然没有签名.所以 s.find('8')<0 将永远是 false 因为不可能是负数.

std::string::npos 的文档:

<块引用>

static const size_type npos = -1;

这是一个特殊值,等于 size_type 类型可表示的最大值.

所以你应该使用 std::string::npos用于检查结果,以避免这种混乱.

if (s.find('8') == std::string::npos)cout <<未找到"<<结束;别的cout <<找到"<<结束;

<小时>

if(s.find('8')==-1) 工作正常,因为 operator== 这里是无符号的,右边的是有符号的.根据算术运算符的规则,

<块引用>
  • 否则,如果无符号操作数的转换等级大于或等于有符号操作数的转换等级,则将有符号操作数转换为无符号操作数的类型.

所以-1会被转换成unsigned,也就是std::string::npos的值,然后一切都按预期工作.

I am trying to find a character within a string but I am getting unexpected results. My understanding is that string::find(char c) returns -1 when it is not found. However, I am getting some unexpected results.

Even though the string does not include an '8', it is still returning true.

std::string s = "123456799";
if(s.find('8')<0)
    cout << "Not Found" << endl;
else
    cout <<  "Found" << endl;

//Output: Found

However, when using == instead the code works as expected.

std::string s = "123456799";
if(s.find('8')==-1)
    cout << "Not Found" << endl;
else
    cout <<  "Found" << endl;

//Output: Not Found

解决方案

My understanding is that string::find(char c) returns -1 when it is not found.

It's not accurate. According to the documentation:

Return value
Position of the first character of the found substring or npos if no such substring is found.

So to be precise, when not found std::string::find will return std::string::npos. The point is that the type of std::string::npos is std::string::size_type, which is an unsigned integer type. Even it's initialized from value of -1, it's not -1; it's still unsigned. So s.find('8')<0 will always be false because it's not possible to be negative.

Documentation of std::string::npos:

static const size_type npos = -1;

This is a special value equal to the maximum value representable by the type size_type.

So you should use std::string::npos for checking the result, to avoid such kind of confusing.

if (s.find('8') == std::string::npos)
    cout << "Not Found" << endl;
else
    cout <<  "Found" << endl;


if(s.find('8')==-1) works fine, because the left-hand operand of operator== here is unsigned, the right-hand one is signed. According to the rules for arithmetic operators,

  • Otherwise, if the unsigned operand's conversion rank is greater or equal to the conversion rank of the signed operand, the signed operand is converted to the unsigned operand's type.

So -1 will be converted to unsigned, which is the value of std::string::npos and then all work as expected.

这篇关于string.find() 在使用 ==-1 时返回 true,但在使用 &lt;0 时返回 false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Rising edge interrupt triggering multiple times on STM32 Nucleo(在STM32 Nucleo上多次触发上升沿中断)
How to use va_list correctly in a sequence of wrapper functions calls?(如何在一系列包装函数调用中正确使用 va_list?)
OpenGL Perspective Projection Clipping Polygon with Vertex Outside Frustum = Wrong texture mapping?(OpenGL透视投影裁剪多边形,顶点在视锥外=错误的纹理映射?)
How does one properly deserialize a byte array back into an object in C++?(如何正确地将字节数组反序列化回 C++ 中的对象?)
What free tiniest flash file system could you advice for embedded system?(您可以为嵌入式系统推荐什么免费的最小闪存文件系统?)
Volatile member variables vs. volatile object?(易失性成员变量与易失性对象?)