4

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?
4

1 に答える 1

6

はい。

文字列の比較演算子は、そのtraits::compare(つまりchar_traits<char>::compare) (C++03 21.3.6.8) に関して定義され、引数の辞書編集順序に基づいて値を返すように指定されています (21.1.1)。

X::compare(p,q,n) ... 結果: [0,n) の各 i について、X::eq(p[i],q[i]) が true の場合は 0。[0,n) の一部の j について X::lt(p[j],q[j]) が true であり、[0,j) の各 i について X::eq(p [i],q[i]) は真です。それ以外の場合は正の値。

実際には、文字列の比較はロケールに依存してはならないことを意味します(私のような一部のロケールでは非辞書編集的である可能性があります)。

于 2012-04-08T20:47:27.143 に答える