【发布时间】:2011-03-18 02:32:54
【问题描述】:
我在下面的函数中发现了一个错误。当 temp = 10 时。它将 temp 转换为字符串 '01'。而不是字符串'10'。我说不出为什么? 将 Num 转换为 Str 有更好的方法吗?谢谢。
这样完成了 Num2Str(),
static bool Num2Str(string& s, const T& value)
{
int temp = static_cast<int>(value); // When temp = 10.
s.push_back(char('0' + temp % 10));
temp /= 10;
while(temp != 0)
{
s.push_back(char('0' + temp % 10));
temp /= 10;
}
if(s.size() == 0)
{
return false;
}
if(s.find_first_not_of("0123456789") != string::npos)
{
return false;
}
return true;
}
【问题讨论】:
-
你的意思不是 itoa 吗? (cplusplus.com/reference/clibrary/cstdlib/itoa)。
-
看起来任何数字都会颠倒过来,而不仅仅是 10。
标签: c++ function typeconverter