C++:比较字符串词典

C++: Comparing strings lexicographical

本文关键字:字符串 比较 C++      更新时间:2023-10-16

使用覆盖的bool operator<(const std::string & rhs)运算符时,字符串是否比较字典顺序?例如:

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?

是的。

字符串的比较运算符是根据其traits::compare(即char_traits<char>::compare)(C++03 21.3.6.8)定义的,该运算符被指定为基于其参数的字典顺序(21.1.1)返回值。

X::compare(p,q,n) ... 产量:如果对于 [0,n] 中的每个 i,则为 0, X::eq(p[i],q[i]) 为真;否则,否定 值,如果,对于 [0,n] 中的某些 j, X::lt(p[j],q[j]) 为真,对于每个 i [0,j)X::eq(p[i],q[i]) 为真;否则为正值。

实际上,这意味着比较字符串不得区分区域设置(在某些区域设置中可能是非字典编纂的,例如我的)。