[英]Smallest lexicographical value of a string
将字符串与operator <进行比较时, 最小的字符串是什么?
更具体地说,什么是比任何其他字符串更小(使用<)的字符串?
空字符串是所有字符串中的“最小”字符串 - 也就是说,它比任何非空字符串都要小。
§21.4.8.4[string :: 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 返回 :
lhs.compare(rhs) < 0
。
§21.4.7.9[string :: compare]:
int compare(const basic_string& str) const noexcept;
1 效果 :确定要比较的字符串的有效长度
rlen
,作为size()
和str.size()
。 然后函数通过调用traits::compare(data(), str.data(), rlen)
比较两个字符串。2 返回 :如果比较结果非零,则返回非零结果。 否则,返回表72中指示的值。
表72 - compare()结果
Condition Return Value size() < str.size() < 0 size() == str.size() 0 size() > str.size() > 0
对于空字符串e
和非空字符串ne
之间的任何比较, rlen
为零,在这种情况下, traits::compare()
被指定为返回零* 。 因此, e.compare(ne)
的结果总是小于每个表72的零,并且e < ne
总是为真。
* 如果“对于[0,n)中的每个i, X::eq(p[i],q[i])
为真X::eq(p[i],q[i])
则指定字符特征的compare()
函数返回零”(§21.2.1[ char.traits.require],表62); 当n == 0
,范围为空,条件为真空。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.