是否有可能构造一个“无限的"结构?细绳? [英] Is it possible to construct an "infinite" string?

查看:91
本文介绍了是否有可能构造一个“无限的"结构?细绳?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否存在任何比任何其他字符串都比较大的真实字符序列?

我首先想到的是这样构造的字符串:

std::basic_string<T>(std::string::max_size(), std::numeric_limits<T>::max())

可以解决问题,但前提是它几乎肯定无法正常工作不是什么大问题.因此,我认为这种骇客技术只能用Unicode来实现.我从未听说过任何迹象表明它确实可能实现,但我也从未听说过它不是可行的,我很好奇. >

有没有关于如何在没有possibly_infinite<basic_string<T>>的情况下实现此目标的想法?

解决方案

我假设您使用字符串的字符值比较字符串. IE.一个字符的作用类似于数字,较长的字符串大于较短的字符串,等等.

有没有真正比所有其他字符串都大的真正字符序列?

否,因为:

  1. 我们假设有一个字符串 s 总是比其他任何字符串大.
  2. 如果您复制 s ,则该副本将等于 s .等于表示不大于".因此,可以有一个不大于 s 的字符串.
  3. 如果您复制 s 并在末尾附加一个字符,则该字符将大于原始的 s .因此,可以有一个大于 s 的字符串.
  4. 这意味着不可能制作 s .

始终大于任何其他字符串的字符串 s 都不存在. s (副本==其他字符串)的副本将等于 s ,等于"表示不大于".
如果最大字符串大小具有合理的限制,则可以存在始终大于或等于任何其他字符串的字符串 s .没有大小限制,可以复制 s ,在末尾附加一个字符,并获得大于 s 的字符串.

在我看来,正确的解决方案是引入某种表示无限大"字符串的特殊字符串对象,并为该对象和标准字符串编写一个比较运算符.另外,在这种情况下,您可能需要自定义字符串类.

可以使字符串始终小于或等于任何其他字符串.零长度字符串就是这样-总是比其他任何东西都小,并且等于其他零长度字符串.

或者您可以编写反直觉的比较例程,其中较短的字符串大于较长的字符串,但是在这种情况下,下一个代码维护者会讨厌您,所以这不是一个好主意.

虽然不确定为什么您会需要类似的东西.

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>>?

解决方案

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.

s there any real sequence of characters that always compares greater than any other string?

No, because:

  1. Let's assume there is a string s that is always greater than any other string.
  2. If you make a copy of s, the copy will be equal to s. Equal means "not greater". Therefore there can be a string that is not greater than s.
  3. If you make a copy of s and append one character at the end, it will be greater than original s. Therefore there can be a string that is greater than s.
  4. Which means, it is not possible to make s.

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.

这篇关于是否有可能构造一个“无限的"结构?细绳?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆