Is it possible to build an infinite string?

Is there any real character sequence that always compares more than any other string?

My first thought was that the line is structured like this:

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

I would do the trick on the condition that the fact that it almost certainly would not work is not such a big problem. Therefore, I suggest that this kind of hacking can only be achieved in Unicode, if it can be achieved at all. I have never heard of anything that could mean that it is really possible, but I also have not heard that this is not so, and I am curious.

Any thoughts on how to achieve this without possibly_infinite<basic_string<T>>?

+3
source share
8 answers

, , . , , ..

s - , ?

, :

  • , s, .
  • s, s. " ". , , s.
  • s , , s. , , , s.
  • , s .

..

s, , . s (copy == other string) s, "equal" " ".
s, , , . s, , , s.

, - , , . , .

, . , , - , .

- , , , .

, - - .

+3

, , , " " , .

+2

Unicode , . Unicode - , 1, 2 4 , . , .

+2

. , :)

+2

, , . , ? - ? <?

:

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

std::lexicographical_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 ) );

compareisson, , :

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;
}
+1

, , , .

.

0

, , ASCII (7F ASCII FF ), , , , , .

0

?

, -, "" .

0

Source: https://habr.com/ru/post/1754855/


All Articles