C++ string::compare 确定字母顺序是否可靠?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1854499/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:24:28  来源:igfitidea点击:

Is string::compare reliable to determine alphabetical order?

c++string

提问by MPelletier

Simply put, if the input is always in the same case (here, lower case), and if the characters are always ASCII, can one use string::compare to determine reliably the alphabetical order of two strings?

简单地说,如果输入总是相同的大小写(这里是小写),并且字符总是 ASCII,那么是否可以使用 string::compare 来可靠地确定两个字符串的字母顺序?

Thus, with stringA.compare(stringB)if the result is 0, they are the same, if it is negative, stringA comes before stringB alphabetically , and if it is positive, stringA comes after?

因此,stringA.compare(stringB)如果结果为 0,则它们相同,如果结果为负,则 stringA 按字母顺序排在 stringB 之前,如果结果为正,则 stringA 排在后面?

采纳答案by Moishe Lettvin

According to the docs at cplusplus.com,

根据 cplusplus.com 上的文档,

The member function returns 0 if all the characters in the compared contents compare equal, a negative value if the first character that does not match compares to less in the object than in the comparing string, and a positive value in the opposite case.

如果比较内容中的所有字符比较相等,则成员函数返回 0,如果不匹配的第一个字符比较对象中的字符小于比较字符串中的字符,则返回负值,反之则返回正值。

So it will sort strings in ASCII order, which will be alphabetical for English strings (with no diacritical marks or other extended characters) of the same case.

所以它会按 ASCII 顺序对字符串进行排序,对于相同大小写的英文字符串(没有变音符号或其他扩展字符),这将是按字母顺序排列的。

回答by James McNellis

Yes, as long as all of the characters in both strings are of the same case, and as long as both strings consist only of letters, this will work.

是的,只要两个字符串中的所有字符大小写相同,并且两个字符串都只包含字母,这将起作用。

compareis a member function, though, so you would call it like so:

compare但是,它是一个成员函数,因此您可以这样调用它:

stringA.compare(stringB);

回答by David Rodríguez - dribeas

In C++, stringis the instantiation of the template class basic_stringwith the default parameters: basic_string<char, char_traits<char>, allocator<char> >. The compare function in the basic_stringtemplate will use the char_traits<TChar>::comparefunction to determine the result value.

在 C++ 中,stringbasic_string具有默认参数的模板类的实例化:basic_string<char, char_traits<char>, allocator<char> >. basic_string模板中的比较函数将使用该char_traits<TChar>::compare函数来确定结果值。

For std::stringthe ordering will be that of the default character code for the implementation (compiler) and that is usually ASCII order. If you require a different ordering (say you want to consider { a, á, à, a } as equivalent), you can instantiate a basic_stringwith your own char_traits<>implementation. providing a different comparefunction pointer.

因为std::string排序将是实现(编译器)的默认字符代码的排序,通常是 ASCII 顺序。如果您需要不同的顺序(假设您想将 { a, á, à, a } 视为等效),您可以basic_string使用您自己的char_traits<>实现实例化 a 。提供不同的compare函数指针。

回答by Thomas Matthews

The specifications for the C and C++ language guarantee for lexical ordering, 'A' < 'B' < 'C' ... < 'Z'. The same is true for lowercase.

C 和 C++ 语言的规范保证词法排序,'A' < 'B' < 'C' ... < 'Z'。对于小写字母也是如此。

The ordering for text digits is also guaranteed: '0' < ... < '9'.

文本数字的排序也得到保证:'0' < ... < '9'。

When working with multiple languages, many people create an array of characters. The array is searched for the character. Instead of comparing characters, the indices are compared.

在使用多种语言时,许多人会创建一个字符数组。在数组中搜索字符。不是比较字符,而是比较索引。

回答by aJ.

yes,

是的

The member function returns 0 if all the characters in the compared contents compare equal, a negative value if the first character that does not match compares to less in the object than in the comparing string, and a positive value in the opposite case.

For string objects, the result of a character comparison depends only on its character code (i.e., its ASCII code), so the result has some limited alphabetical or numerical ordering meaning.

如果比较内容中的所有字符比较相等,则成员函数返回 0,如果不匹配的第一个字符比较对象中的字符小于比较字符串中的字符,则返回负值,反之则返回正值。

对于字符串对象,字符比较的结果仅取决于其字符代码(即其 ASCII 代码),因此结果具有一些有限的字母或数字排序含义。