您应该尝试说明您打算实现什么以及您的要求是什么。特别是,它必须是一个字符串吗?域名有限制吗?他们需要比较<
吗?
您可以使用非字符串类型:
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 ) );
如果您可以使用任何其他比较函子并且您的域有一些无效,您可以使用它来获得优势:
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;
}