是否有可能构建一个“无限”的矩阵?细绳?

发布于 2024-09-10 00:59:48 字数 410 浏览 2 评论 0 原文

是否存在任何真实的字符序列总是比任何其他字符串进行比较?

我的第一个想法是,像这样构造的字符串:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

望喜 2024-09-17 00:59:48

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

是否存在任何真实的字符序列,其比较结果总是大于任何其他字符串?

不,因为:

  1. 假设有一个字符串 s 始终大于任何其他字符串。
  2. 如果您复制s,则该副本将等于s。等于意味着“不大于”。因此,可以有一个不大于s的字符串。
  3. 如果您复制 s 并在末尾添加一个字符,则它将大于原始 s。因此,可能存在大于s的字符串。
  4. 这意味着,不可能制造s

不可能存在始终大于任何其他字符串的字符串ss 的副本(副本 == 其他字符串)将等于 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.

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.

染火枫林 2024-09-17 00:59:48

您可能需要一个自定义比较器,为其定义一个神奇的“无限字符串”值,并且该值始终将该值视为大于任何其他值。

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.

℉絮湮 2024-09-17 00:59:48

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.

甜点 2024-09-17 00:59:48

。你是怎么做到的,我不知道:)

Yes. How you do it, I have no idea :)

筱武穆 2024-09-17 00:59:48

您应该尝试说明您想要实现的目标以及您的要求是什么。特别是,它必须是一个字符串吗?域名有任何限制吗?它们需要与 < 进行比较吗?

您可以使用非字符串类型:

struct infinite_string {};
bool operator<( std::string const & , infinite_string const & ) {
   return true;
}
bool operator<( infinite_string const &, std::string const & ) {
   return false;
}

如果您可以使用 std::lexicographyal_compare 并且不需要将其存储为字符串,那么您可以编写一个无限迭代器:

template <typename CharT>
struct infinite_iterator
{
   CharT operator*() { return std::numeric_limits<CharT>::max(); }
   infinite_iterator& operator++() { return *this; }
   bool operator<( const infinite_iterator& ) { return true; }
   // all other stuff to make it proper
};
assert( std::lexicographical_compare( str.begin(), str.end(), 
                              infinite_iterator, infinite_iterator ) );

如果您可以使用任何其他比较函子和您的域有一些无效,您可以利用它来发挥您的优势:

namespace detail {
   // assume that "\0\0\0\0\0" is not valid in your domain
   std::string const infinite( 5, 0 ); 
}
bool compare( std::string const & lhs, std::string const & rhs ) {
   if ( lhs == detail::infinite ) return false;
   if ( rhs == detail::infinite ) return true;
   return lhs < rhs;
}

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:

struct infinite_string {};
bool operator<( std::string const & , infinite_string const & ) {
   return true;
}
bool operator<( infinite_string const &, std::string const & ) {
   return false;
}

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:

template <typename CharT>
struct infinite_iterator
{
   CharT operator*() { return std::numeric_limits<CharT>::max(); }
   infinite_iterator& operator++() { return *this; }
   bool operator<( const infinite_iterator& ) { return true; }
   // all other stuff to make it proper
};
assert( std::lexicographical_compare( str.begin(), str.end(), 
                              infinite_iterator, infinite_iterator ) );

If you can use any other comparisson functor and your domain has some invalid you can use that to your advantage:

namespace detail {
   // assume that "\0\0\0\0\0" is not valid in your domain
   std::string const infinite( 5, 0 ); 
}
bool compare( std::string const & lhs, std::string const & rhs ) {
   if ( lhs == detail::infinite ) return false;
   if ( rhs == detail::infinite ) return true;
   return lhs < rhs;
}
摇划花蜜的午后 2024-09-17 00:59:48

如果您需要在没有边界的对象空间内进行人工边界,标准技巧是添加一个额外的元素并定义一个新的比较运算符来强制您的属性。

或者实现惰性字符串。

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.

坦然微笑 2024-09-17 00:59:48

好吧,如果您要动态构造一个与您要比较的字符串长度相同的字符串,并用可用的最高 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.

故人的歌 2024-09-17 00:59:48

你的比较器是什么?

在此基础上,您可以构建晶格“顶部”的东西。

What's your comparator?

Based on that, you can construct something that is the 'top' of your lattice.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文