gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 17:36:26 24 4
gpt4 key购买 nike

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

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

最佳答案

根据 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.

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

关于c++ - string::compare 确定字母顺序是否可靠?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1854499/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com