简体   繁体   English

string :: compare是否可靠,以确定字母顺序?

[英]Is string::compare reliable to determine alphabetical order?

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? 因此,如果结果为0,则使用stringA.compare(stringB) ,它们是相同的,如果是负数,则stringA按字母顺序排在stringB之前,如果是正数,则stringA会在之后?

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顺序对字符串进行排序,这对于相同案例的英语字符串(没有变音符号或其他扩展字符)将按字母顺序排列。

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. 是的,只要两个字符串中的所有字符具有相同的大小写,并且只要两个字符串仅由字母组成,这将起作用。

compare is a member function, though, so you would call it like so: compare是一个成员函数,所以你可以像这样调用它:

stringA.compare(stringB);

In C++, string is the instantiation of the template class basic_string with the default parameters: basic_string<char, char_traits<char>, allocator<char> > . 在C ++中, string是模板类basic_string的实例化,其默认参数为: basic_string<char, char_traits<char>, allocator<char> > The compare function in the basic_string template will use the char_traits<TChar>::compare function to determine the result value. basic_string模板中的compare函数将使用char_traits<TChar>::compare函数来确定结果值。

For std::string the ordering will be that of the default character code for the implementation (compiler) and that is usually ASCII order. 对于std::string ,排序将是实现(编译器)的默认字符代码的顺序,通常是ASCII顺序。 If you require a different ordering (say you want to consider { a, á, à, â } as equivalent), you can instantiate a basic_string with your own char_traits<> implementation. 如果您需要不同的顺序(假设您要将{a,á,à,â}视为等效),则可以使用自己的char_traits<>实现来实例化basic_string providing a different compare function pointer. 提供不同的compare函数指针。

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. 如果比较内容中的所有字符比较相等,则成员函数返回0;如果不匹配的第一个字符在对象中比在比较字符串中比较少则为负值,在相反情况下为正值。

For string objects, the result of a character comparison depends only on its character code (ie, its ASCII code ), so the result has some limited alphabetical or numerical ordering meaning. 对于字符串对象,字符比较的结果仅取决于其字符代码(即其ASCII代码 ),因此结果具有一些有限的字母或数字排序含义。

The specifications for the C and C++ language guarantee for lexical ordering, 'A' < 'B' < 'C' ... < 'Z'. C和C ++语言的规范保证了词汇排序,'A'<'B'<'C'...... <'Z'。 The same is true for lowercase. 小写也是如此。

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. 不是比较字符,而是比较索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM