4

我正在尝试将数字类型转换为带有前导 '0' 的固定长度字符串表示形式(在只能处理字符串的 GUI 中使用 - 数字的字典排序真的很尴尬)。

与其在需要的地方创建字符串,我认为应该可以制作一个很好的模板供其他人使用,该模板也适用于不同的算术类型。

template <typename T, typename std::enable_if<std::is_arithmetic<T>::value, T>::type* = nullptr>
std::string arithmeticToMaxLengthString(const T& value)
{
  std::string number, result;
  number = std::to_string( value );
  result = lpad( '0', std::numeric_limits<decltype(value)>::digits10 + 1, number );
  return result;
}

std::string lpad( char filler, std::string::size_type length, const std::string& input)
{
  std::string result;
  if( input.length() < length )
  {
     result.append( lenght - input.lenght(), filler ).append( input );
     return result;
  }
  return input;
}

到现在为止还挺好。它编译,我可以使用它。但:

当我尝试使用它时,我得到了一个惊喜。使用 int32_t 调用此函数

typedef int int32_t;

值为 100 的结果是字符串“100”,长度为 3。

我很确定代码库中不存在其他名称如此糟糕的模板(我已经搜索了所有模板以及所有链接库),但我似乎无法理解我生成的代码有什么问题。为了验证代码是否正确,我刚刚将它复制粘贴到它被调用的地方,现在基本上看起来像这样:

myDataClass{
  int32_t     quantity;
  std::string numberAsString;
}

void setNumberString( myDataClass data )
{
   std::string result    = arithmeticToMaxLengthString( data.quantity );
   std::string reference = lpad( '0', std::numeric_limits<decltype(value)>::digits10 + 1, std::to_string(data.quantity) );
   std::cout << "quantity<" << data.quantity << "> numericLimits<" << std::numeric_limits<decltype(value)>::digits10 + 1 << ">" << std::endl;
   std::cout << "result  template: length<" << result.lenght()    << "> content<" << result    << ">" << std::endl;
   std::cout << "result reference: length<" << reference.lenght() << "> content<" << reference << ">" << std::endl;
}

现在据我了解,模板“arithmeticToMaxLengthString”以int32_t作为参数的类型调用,这是一个普通的int,其中“numeric_limits::digits10 + 1”给了我10。但是当现在调用这个函数时,数量为说 100 结果不同。

quantity<100> numericLimits<10>
result  template: length<3> content<100>
result reference: length<10> content<0000000100>

我是否缺少有关模板、enable_if 或 numeric_limits 的内容?模板有何不同(除了明显的 enable_if 之外)?

4

2 回答 2

2

您的问题发生是因为您正在传递对arithmeticToMaxLengthString函数的引用。由于此处另一个答案中列出的原因,std::numeric_limits不适用于参考。

因此,我建议您使用模板类型T来获取数值限制,而不是使用decltype(value)

result = lpad('0', std::numeric_limits<T>::digits10 + 1, number);

它不仅解决了问题,而且看起来也更加优雅。

于 2020-02-16T22:40:18.903 回答
1

这并不是您问题的真正答案,而是关于如何使用STL.

#include <iomanip>
#include <sstream>

template <typename T, unsigned N = 5>
std::string to_string_with_leading_0(T element) {
    std::stringstream ss;
    ss << std::setw(N) << std::setfill('0') << element;
    return ss.str();
}
于 2020-02-16T21:43:54.723 回答