简体   繁体   English

C++:比较字符串字典顺序

[英]C++: Comparing strings lexicographical

Are strings compared lexicographical when using the overriden bool operator<(const std::string & rhs) operator?使用覆盖的bool operator<(const std::string & rhs) operator 时,字符串是否按字典顺序进行比较? In example:例如:

std::string str1 = "aabbcc"
std::string str2 = "bbaacc"

(str1 < str2) == std::lexicographical_compare(str1.begin(),str1.end(),str2.begin(),str2.end()) // is this statement true?

Yes.是的。

String's comparison operators are defined in terms of its traits::compare (that is char_traits<char>::compare ) (C++03 21.3.6.8) which is specified to return a value based on the lexicographical ordering of its arguments (21.1.1).字符串的比较运算符是根据其traits::compare (即char_traits<char>::compare )(C++03 21.3.6.8) 定义的,它指定返回一个基于其 arguments (21.1) 的字典顺序的值.1).

X::compare(p,q,n)... yields: 0 if for each i in [0,n), X::eq(p[i],q[i]) is true; X::compare(p,q,n)... 产生:如果对于 [0,n] 中的每个 i,X::eq(p[i],q[i]) 为真,则为 0; else, a negative value if, for some j in [0,n), X::lt(p[j],q[j]) is true and for each i in [0,j) X::eq(p[i],q[i]) is true;否则,如果对于 [0,n) 中的某些 j,X::lt(p[j],q[j]) 为真且对于 [0,j) 中的每个 i X::eq(p [i],q[i]) 为真; else a positive value.否则为正值。

In effect, it means comparing string must not be locale sensitive (which could be non-lexicographical in some locales, such as mine).实际上,这意味着比较字符串不能对区域设置敏感(在某些区域设置中可能是非字典顺序的,例如我的)。

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

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