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-1when 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.
相关文章