字符串的最小词典价值 [英] Smallest lexicographical value of a string
问题描述
比较带有运算符<的字符串时,最小字符串是什么?
When comparing strings with operator <, what is the smallest string?
更具体地说,什么是字符串比其他任何字符串都小(使用<)?
To be more specific, what is a string that is smaller (using <) than any other string?
推荐答案
空字符串是所有字符串中最小的-即,它比任何非空字符串都少。
The empty string is the "smallest" of all strings - that is, it compares less than any non-empty string.
§21.4.8.4[string :: op<]:
§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]:
§21.4.7.9 [string::compare]:
int compare(const basic_string& str) const noexcept;
1 效果:确定有效长度 rlen
到
的字符串与 size()
和 str.size()的最小值相比进行比较code>。然后,函数
通过调用 traits :: compare(data(),
比较两个字符串。
str.data(),rlen)
1 Effects: Determines the effective length rlen
of the strings to
compare as the smallest of size()
and str.size()
. The function
then compares the two strings by calling traits::compare(data(),
str.data(), rlen)
.
2 返回:如果
比较的结果为非零,则为非零结果。否则,返回
表72中指示的值。
2 Returns: The nonzero result if the result of the comparison is nonzero. Otherwise, returns a value as indicated in Table 72.
表72 — compare()结果
Table 72 — compare() results
Condition Return Value
size() < str.size() < 0
size() == str.size() 0
size() > str.size() > 0
用于空字符串之间的任何比较 e
和非空字符串 ne
, rlen
为零,在这种情况下 traits :: compare()
被指定为返回零 * 。因此,每个表72的 e.compare(ne)
的结果始终小于零,并且 e < ne
始终为真。
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.
* 如果对于[0,n)中的每个i, X :: eq(p [ i],q [i])
是真实的(第21.2.1节[char.traits.require],表62);当 n == 0
时,该范围为空,并且条件空虚为真。
* 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.
这篇关于字符串的最小词典价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!