空字符串是所有字符串中“最小”的——也就是说,它比任何非空字符串都小。
§21.4.8.4 [字符串::op
template<class charT, class traits, class Allocator>
bool operator< (const basic_string<charT,traits,Allocator>& lhs,
const basic_string<charT,traits,Allocator>& rhs) noexcept;
1 Returns: lhs.compare(rhs) < 0
.
§21.4.7.9 [字符串::比较]:
int compare(const basic_string& str) const noexcept;
1 Effects:确定有效长度rlen
的字符串到
比较最小的size()
and str.size()
。功能
然后通过调用比较两个字符串traits::compare(data(),
str.data(), rlen)
.
2 Returns:如果结果为非零结果
比较非零。否则,返回一个值,如
表 72.
表 72 —compare() 结果
Condition Return Value
size() < str.size() < 0
size() == str.size() 0
size() > str.size() > 0
For any comparison between an empty string e
and a non-empty string ne
, rlen
is zero, in which case traits::compare()
is specified to return zero*. Hence, the result of e.compare(ne)
is always less than zero per table 72, and e < ne
is always true.
* The compare()
function of character traits is specified to return zero if "for each i in [0,n), X::eq(p[i],q[i])
is true" (§21.2.1 [char.traits.require], Table 62); when n == 0
, the range is empty, and the condition is vacuously true.