[英]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.