English 中文(简体)
哪些原因导致回报价值大于0、1或-1?
原标题:What causes strcmp to return a value other than 0, 1, or -1?
  • 时间:2024-04-26 02:15:39
  •  标签:
  • c++
  • strcmp

I have this piece of code. When I run it, it returns 13, -13, and 0. Isn t it supposed to return 1 if the str1 is greater than str2 and -1 if str1 is less than str2.

我获得13、13和0。

#include <iostream>
#include <string>
#include <cstring>

int main() {

    char str1[] = "Megadeth";
    char str2[] = "Metallica";

    // Should return -1 because "Megadeth" < "Metallica" lexicographically
    int result = strcmp(str1, str2);

    std::cout << "Comparing " << str1 << " and " << str2 << ": " << result << std::endl;

    // Should return 1 because "Metallica" > "Megadeth" lexicographically
    result = strcmp(str2, str1);

    std::cout << "Comparing " << str2 << " and " << str1 << ": " << result << std::endl;

    // Should return 0 because "Megadeth" = "Megadeth" lexicographically
    result = strcmp(str1, str1);

    std::cout << "Comparing " << str1 << " and " << str1 << ": " << result << std::endl;

    return 0;
}

结果是:

Comparing Megadeth and Metallica: -13
Comparing Metallica and Megadeth: 13
Comparing Megadeth and Megadeth: 0

我刚刚在互联网上接受过辅导,他们说,这本应是回去的。 我不知道造成这种情况的原因。

最佳回答

<代码>strcmp未定义返回1,0,或-1。 如果前者在地理上比后者高,则其结果要么是积极的,如果前者在地理上比后者低一些,则是否定的。 只界定了标识,即0,而不是数量。

www.un.org/Depts/DGACM/index_spanish.htm

The strcmp() and strncmp() functions return an integer greater than, equal to, or less than 0, according as the string s1 is greater than, equal to, or less than the string s2. The comparison is done using unsigned characters, so that 200 is greater than ` .

首先寄来的信函,至少是关于您执行情况的信函,被视为较小的。 因此,第一封在<条码>、Megadeth和<条码>之间作错配的信函是<条码>g<>。 相对于<代码>t,t,在<代码>Metallica为第二时,其结果为负数。

问题回答

当C成为新语言时,与其他语言相比,它正在发出呼吁并鼓励。 因此,《<条码>>>(>>>>>>>/代码”的首批实施就是这样:

while (*s1++ == *s2++) ;
return *--s1 - *--s2;

选择了正值/零/负回报值,以便采用类似标准。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签