是否存在任何真实的字符序列总是比任何其他字符串进行比较?
我的第一个想法是,像这样构造的字符串:
std::basic_string<T>(std::string::max_size(), std::numeric_limits<T>::max())
可以解决问题,前提是它几乎肯定无法工作这一事实并不是一个大问题。所以我认为这种黑客行为只能在 Unicode 中完成(如果可以的话)。我从未听说过任何事情表明它确实可能,但我也没有听说过它不可能,我很好奇。
对于如何在没有 possible_infinite>
的情况下实现这一目标有什么想法吗?
Is there any real sequence of characters that always compares greater than any other string?
My first thought was that a string constructed like so:
std::basic_string<T>(std::string::max_size(), std::numeric_limits<T>::max())
Would do the trick, provided that the fact that it would almost definitely fail to work isn't such a big issue. So I presume this kind of hackery could only be accomplished in Unicode, if it can be accomplished at all. I've never heard of anything that would indicate that it really is possible, but neither have I heard tell that it isn't, and I'm curious.
Any thoughts on how to achieve this without a possibly_infinite<basic_string<T>>
?
发布评论
评论(8)
我假设您使用字符串的字符值来比较字符串。即一个字符的作用类似于数字,较长的字符串大于较短的字符串,等等。
不,因为:
即
不可能存在始终大于任何其他字符串的字符串s。 s 的副本(副本 == 其他字符串)将等于 s,“等于”意味着“不大于”。
如果最大字符串大小有合理的限制,则可以存在始终大于或等于任何其他字符串的字符串s。如果没有大小限制,则可以获取 s 的副本,在末尾附加一个字符,并获得大于 s 的字符串。
在我看来,正确的解决方案是引入某种表示无限“大”字符串的特殊字符串对象,并为该对象和标准字符串编写一个比较运算符。另外,在这种情况下,您可能需要自定义字符串类。
可以使字符串始终小于或等于任何其他字符串。零长度字符串正是如此 - 总是小于其他任何字符串,并且等于其他零长度字符串。
或者您可以编写反直觉的比较例程,其中较短的字符串大于较长的字符串,但在这种情况下,下一个代码维护者会讨厌您,所以这不是一个好主意。
但不确定为什么你需要这样的东西。
I assume that you compare strings using their character value. I.e. one character acts like a digit, a longer string is greater than shorter string, etc.
No, because:
I.e.
A string s that is always greater than any other string cannot exist. A copy of s (copy == other string) will be equal to s, and "equal" means "not greater".
A string s that is always greater or equal to any other string, can exist if a maximum string size has a reasonable limit. Without a size limit, it will be possible to take a copy of s, append one character at the end, and get a string that is greater than s.
In my opinion, the proper solution would be to introduce some kind of special string object that represents infinitely "large" string, and write a comparison operator for that object and standard string. Also, in this case you may need custom string class.
It is possible to make string that is always less or equal to any other string. Zero length string will be exactly that - always smaller than anything else, and equal to other zero-length strings.
Or you could write counter-intuitive comparison routine where shorter string is greater than longer string, but in this case next code maintainer will hate you, so it is not a good idea.
Not sure why would you ever need something like that, though.
您可能需要一个自定义比较器,为其定义一个神奇的“无限字符串”值,并且该值始终将该值视为大于任何其他值。
You probably need a custom comparator, for which you define a magic "infinite string" value and which will always treat that value as greater than any other.
Unicode 解决了很多问题,但不是那个问题。 Unicode 只是字符的不同编码,1、2 或 4 个字节,它们仍然存储在普通数组中。当你找到一台内存无限的机器时,你可以使用无限字符串。
Unicode solves a lot of problems, but not that one. Unicode is just a different encoding for a character, 1, 2 or 4 bytes, they are still stored in a plain array. You can use infinite strings when you find a machine with infinite memory.
是。你是怎么做到的,我不知道:)
Yes. How you do it, I have no idea :)
您应该尝试说明您想要实现的目标以及您的要求是什么。特别是,它必须是一个字符串吗?域名有任何限制吗?它们需要与
<
进行比较吗?您可以使用非字符串类型:
如果您可以使用
std::lexicographyal_compare
并且不需要将其存储为字符串,那么您可以编写一个无限迭代器:如果您可以使用任何其他比较函子和您的域有一些无效,您可以利用它来发挥您的优势:
You should try to state what you intend to achieve and what your requirements are. In particular, does it have to be a string? is there any limitation on the domain? do they need to be compared with
<
?You can use a non-string type:
If you can use
std::lexicographical_compare
and you don't need to store it as a string, then you can write an infinite iterator:If you can use any other comparisson functor and your domain has some invalid you can use that to your advantage:
如果您需要在没有边界的对象空间内进行人工边界,标准技巧是添加一个额外的元素并定义一个新的比较运算符来强制您的属性。
或者实现惰性字符串。
if you need an artificial bound within a space of objects that isn't bounded, the standard trick is to add an extra element and define a new comparison operator that enforces your property.
Or implement lazy strings.
好吧,如果您要动态构造一个与您要比较的字符串长度相同的字符串,并用可用的最高 ASCII 代码(对于普通 ASCII 为 7F,对于扩展 FF)填充它,那么您将保证该字符串比较等于或大于您比较的那个。
Well if you were to dynamically construct a string of equal length as the one that you are comparing to and fill it with the highest ASCII code available (7F for normal ASCII or FF for extended) you would be guaranteed that this string would compare equal to or greater than the one you compare it to.
你的比较器是什么?
在此基础上,您可以构建晶格“顶部”的东西。
What's your comparator?
Based on that, you can construct something that is the 'top' of your lattice.