将 int 转换为 Glib::ustring w/o stringstream
Convert int to Glib::ustring w/o stringstream
我需要将整数转换为 Glib::ustring,但我不想使用字符串流。并不是说 stringstream 有任何内在的错误,但我不希望另一个库 just 来完成这样一个简单的任务。
我的第一直觉是编写一个带有大 'ol if 语句的函数,或者为每个数字使用一个字符数组,但必须有一些更简洁的东西。还有其他选择吗?
编辑:此方法旨在完全避免使用 STL,因为 cases/libraries 有必要这样做。但是,Glib::ustring 确实确实使用了 STL,仅供参考。如果您正在使用其他一些自定义字符串 class,或者只是觉得 Glib 很聪明,这可能仍然会派上用场。
是的,这实际上是可能的,尽管它确实需要自定义函数。到目前为止,这工作得很好,除了显而易见的 Glib::ustring 之外,不需要任何其他库。您可以替换任何其他支持字符的字符串 class,并相应地调整 Glib::ustring 行。
Glib::ustring int_to_ustring(int num)
{
bool neg = false;
int sub = 0;
char digit;
//This is what we'll return.
Glib::ustring str = "";
//If number is 0, the math won't work. Just return the string "0".
if(num == 0)
{
str = "0";
return str;
}
//Else, if the number is negative...
else if(num < 0)
{
//Store that information and make the number positive.
neg = true;
num = abs(num);
}
//Determine place value.
int pv = 0;
do
{
//Divide by a power of ten and trunicate decimal.
sub = num / pow(10, pv);
//Increase pv.
pv++;
}
//If we got zero, then we're too large.
while(sub != 0);
//NOTE: The above seems to make the place value two-too-large?
//Loop backwards through the place values.
for(pv; pv >= 0; pv--)
{
sub = num / pow(10, pv);
num -= sub*(pow(10, pv));
if(sub < 0 || sub > 10)
{
//Throw an error. I'm just using this as a placeholder.
std::cout << "Something went really weird." << std::endl;
}
//The char code for the digit is always 48 more than the digit.
digit = sub + 48;
//If this isn't a leading zero...
if(!(str == "" && digit == '0'))
{
//This is the best way to push a char to a ustring.
str.insert(str.end(), digit);
}
}
//If that number was negative, insert the negative sign.
if(neg)
str.insert(str.begin(), '-');
return str;
}
(顺便说一句,欢迎提出改进建议!我希望能提高效率。)
Glib::ustring
提供了一个 format
静态函数,它简单地将你抛给它的任何东西(最多 8 个参数,似乎还没有可变参数模板)转发给一个字符串流和 returns格式化字符串:
Glib::ustring text = Glib::ustring::format(123456);
从 c++11 开始,标准库也有一个重载的 to_string
方法来转换整数和浮点数
Glib::ustring text(std::to_string(123456));
我需要将整数转换为 Glib::ustring,但我不想使用字符串流。并不是说 stringstream 有任何内在的错误,但我不希望另一个库 just 来完成这样一个简单的任务。
我的第一直觉是编写一个带有大 'ol if 语句的函数,或者为每个数字使用一个字符数组,但必须有一些更简洁的东西。还有其他选择吗?
编辑:此方法旨在完全避免使用 STL,因为 cases/libraries 有必要这样做。但是,Glib::ustring 确实确实使用了 STL,仅供参考。如果您正在使用其他一些自定义字符串 class,或者只是觉得 Glib 很聪明,这可能仍然会派上用场。
是的,这实际上是可能的,尽管它确实需要自定义函数。到目前为止,这工作得很好,除了显而易见的 Glib::ustring 之外,不需要任何其他库。您可以替换任何其他支持字符的字符串 class,并相应地调整 Glib::ustring 行。
Glib::ustring int_to_ustring(int num)
{
bool neg = false;
int sub = 0;
char digit;
//This is what we'll return.
Glib::ustring str = "";
//If number is 0, the math won't work. Just return the string "0".
if(num == 0)
{
str = "0";
return str;
}
//Else, if the number is negative...
else if(num < 0)
{
//Store that information and make the number positive.
neg = true;
num = abs(num);
}
//Determine place value.
int pv = 0;
do
{
//Divide by a power of ten and trunicate decimal.
sub = num / pow(10, pv);
//Increase pv.
pv++;
}
//If we got zero, then we're too large.
while(sub != 0);
//NOTE: The above seems to make the place value two-too-large?
//Loop backwards through the place values.
for(pv; pv >= 0; pv--)
{
sub = num / pow(10, pv);
num -= sub*(pow(10, pv));
if(sub < 0 || sub > 10)
{
//Throw an error. I'm just using this as a placeholder.
std::cout << "Something went really weird." << std::endl;
}
//The char code for the digit is always 48 more than the digit.
digit = sub + 48;
//If this isn't a leading zero...
if(!(str == "" && digit == '0'))
{
//This is the best way to push a char to a ustring.
str.insert(str.end(), digit);
}
}
//If that number was negative, insert the negative sign.
if(neg)
str.insert(str.begin(), '-');
return str;
}
(顺便说一句,欢迎提出改进建议!我希望能提高效率。)
Glib::ustring
提供了一个 format
静态函数,它简单地将你抛给它的任何东西(最多 8 个参数,似乎还没有可变参数模板)转发给一个字符串流和 returns格式化字符串:
Glib::ustring text = Glib::ustring::format(123456);
从 c++11 开始,标准库也有一个重载的 to_string
方法来转换整数和浮点数
Glib::ustring text(std::to_string(123456));