I have some numbers of different length (like 1, 999, 76492, so on) and I want to convert them all to strings with a common length (for example, if the length is 6, then those strings will be: '000001', '000999', '076492').
我有一些不同长度的数字(比如1,999,76492等等),我想把它们都转换成具有相同长度的字符串(例如,如果长度是6,那么这些字符串将是:'000001','000999','076492')。
In other words, I need to add correct amount of leading zeros to the number.
换句话说,我需要在数字中加入正确数量的前导0。
int n = 999;
string str = some_function(n,6);
//str = '000999'
Is there a function like this in C++?
c++中有这样的函数吗?
7 个解决方案
#1
48
or using the stringstreams:
或使用stringstreams:
#include <sstream>
#include <iomanip>
std::stringstream ss;
ss << std::setw(10) << std::setfill('0') << i;
std::string s = ss.str();
I compiled the information I found on arachnoid.com because I like the type-safe way of iostreams more. Besides, you can equally use this code on any other output stream.
我收集了在arachnoid.com上找到的信息,因为我更喜欢iostreams的类型安全方式。此外,您可以在任何其他输出流上同样地使用此代码。
#3
8
One thing that you may want to be aware of is the potential locking that may go on when you use the stringstream
approach. In the STL that ships with Visual Studio 2008, at least, there are many locks taken out and released as various locale information is used during formatting. This may, or may not, be an issue for you depending on how many threads you have that might be concurrently converting numbers to strings...
您可能需要注意的一件事是使用stringstream方法时可能会发生的潜在锁定。至少在与Visual Studio 2008一起发布的STL中,有许多锁被取出并释放,因为在格式化过程中使用了各种语言环境信息。这对您来说可能是问题,也可能不是问题,这取决于您有多少线程可以同时将数字转换为字符串……
The sprintf
version doesn't take any locks (at least according to the lock monitoring tool that I'm developing at the moment...) and so might be 'better' for use in concurrent situations.
sprintf版本不使用任何锁(至少根据我目前正在开发的锁监视工具……),所以在并发情况下可能会“更好”。
I only noticed this because my tool recently spat out the 'locale' locks as being amongst the most contended for locks in my server system; it came as a bit of a surprise and may cause me to revise the approach that I've been taking (i.e. move back towards sprintf
from stringstream
)...
我之所以注意到这一点,是因为我的工具最近抛出了“locale”锁,它是我的服务器系统中争夺最多的锁之一;这让我有点意外,可能会让我修改我所采取的方法(即从stringstream返回sprintf)……
#4
3
stringstream will do (as xtofl pointed out). Boost format is a more convenient replacement for snprintf.
stringstream将会做(正如xtofl所指出的)。Boost格式是snprintf的更方便的替换。
#5
3
This method doesn't use streams nor sprintf. Other than having locking problems, streams incur a performance overhead and is really an overkill. For streams the overhead comes from the need to construct the steam and stream buffer. For sprintf, the overhead comes from needing to interpret the format string. This works even when n is negative or when the string representation of n is longer than len. This is the FASTEST solution.
此方法不使用流或sprintf。除了有锁定问题之外,流还会导致性能开销,这实际上是一种过度消耗。对于流,开销来自于构建蒸汽和流缓冲区的需要。对于sprintf,开销来自于需要解释格式字符串。即使当n是负的或者当n的字符串表示比len长时,它也能工作。这是最快的解决方案。
inline string some_function(int n, int len)
{
string result(len--, '0');
for (int val=(n<0)?-n:n; len>=0&&val!=0; --len,val/=10)
result[len]='0'+val%10;
if (len>=0&&n<0) result[0]='-';
return result;
}
#6
2
There are many ways of doing this. The simplest would be:
有很多方法可以做到这一点。最简单的是:
int n = 999;
char buffer[256]; sprintf(buffer, "%06d", n);
string str(buffer);
#7
1
sprintf is the C-like way of doing this, which also works in C++.
sprintf是一种类似C的方式,在c++中也适用。
In C++, a combination of a stringstream and stream output formatting (see http://www.arachnoid.com/cpptutor/student3.html ) will do the job.
在c++中,将stringstream和流输出格式(请参见http://www.arachnoid.com/cpptutor/student3.html)结合使用。
#1
48
or using the stringstreams:
或使用stringstreams:
#include <sstream>
#include <iomanip>
std::stringstream ss;
ss << std::setw(10) << std::setfill('0') << i;
std::string s = ss.str();
I compiled the information I found on arachnoid.com because I like the type-safe way of iostreams more. Besides, you can equally use this code on any other output stream.
我收集了在arachnoid.com上找到的信息,因为我更喜欢iostreams的类型安全方式。此外,您可以在任何其他输出流上同样地使用此代码。
#2
#3
8
One thing that you may want to be aware of is the potential locking that may go on when you use the stringstream
approach. In the STL that ships with Visual Studio 2008, at least, there are many locks taken out and released as various locale information is used during formatting. This may, or may not, be an issue for you depending on how many threads you have that might be concurrently converting numbers to strings...
您可能需要注意的一件事是使用stringstream方法时可能会发生的潜在锁定。至少在与Visual Studio 2008一起发布的STL中,有许多锁被取出并释放,因为在格式化过程中使用了各种语言环境信息。这对您来说可能是问题,也可能不是问题,这取决于您有多少线程可以同时将数字转换为字符串……
The sprintf
version doesn't take any locks (at least according to the lock monitoring tool that I'm developing at the moment...) and so might be 'better' for use in concurrent situations.
sprintf版本不使用任何锁(至少根据我目前正在开发的锁监视工具……),所以在并发情况下可能会“更好”。
I only noticed this because my tool recently spat out the 'locale' locks as being amongst the most contended for locks in my server system; it came as a bit of a surprise and may cause me to revise the approach that I've been taking (i.e. move back towards sprintf
from stringstream
)...
我之所以注意到这一点,是因为我的工具最近抛出了“locale”锁,它是我的服务器系统中争夺最多的锁之一;这让我有点意外,可能会让我修改我所采取的方法(即从stringstream返回sprintf)……
#4
3
stringstream will do (as xtofl pointed out). Boost format is a more convenient replacement for snprintf.
stringstream将会做(正如xtofl所指出的)。Boost格式是snprintf的更方便的替换。
#5
3
This method doesn't use streams nor sprintf. Other than having locking problems, streams incur a performance overhead and is really an overkill. For streams the overhead comes from the need to construct the steam and stream buffer. For sprintf, the overhead comes from needing to interpret the format string. This works even when n is negative or when the string representation of n is longer than len. This is the FASTEST solution.
此方法不使用流或sprintf。除了有锁定问题之外,流还会导致性能开销,这实际上是一种过度消耗。对于流,开销来自于构建蒸汽和流缓冲区的需要。对于sprintf,开销来自于需要解释格式字符串。即使当n是负的或者当n的字符串表示比len长时,它也能工作。这是最快的解决方案。
inline string some_function(int n, int len)
{
string result(len--, '0');
for (int val=(n<0)?-n:n; len>=0&&val!=0; --len,val/=10)
result[len]='0'+val%10;
if (len>=0&&n<0) result[0]='-';
return result;
}
#6
2
There are many ways of doing this. The simplest would be:
有很多方法可以做到这一点。最简单的是:
int n = 999;
char buffer[256]; sprintf(buffer, "%06d", n);
string str(buffer);
#7
1
sprintf is the C-like way of doing this, which also works in C++.
sprintf是一种类似C的方式,在c++中也适用。
In C++, a combination of a stringstream and stream output formatting (see http://www.arachnoid.com/cpptutor/student3.html ) will do the job.
在c++中,将stringstream和流输出格式(请参见http://www.arachnoid.com/cpptutor/student3.html)结合使用。