截断 C++由 ostringstream、iomanip:setw 生成的字符串字段
在 C++ 中,我需要带前导零的整数的字符串表示形式,其中表示形式有 8 位且不超过 8 位,如有必要,会截断右侧的数字。我想我可以只使用 ostringstream 和 iomanip.setw() 来完成此操作,如下所示:
int num_1 = 3000;
ostringstream out_target;
out_target << setw(8) << setfill('0') << num_1;
cout << "field: " << out_target.str() << " vs input: " << num_1 << endl;
这里的输出是:
field: 00003000 vs input: 3000
非常好!但是,如果我尝试更大的数字,setw 会让输出增长到超过 8 个字符:
int num_2 = 2000000000;
ostringstream out_target;
out_target << setw(8) << setfill('0') << num_2;
cout << "field: " << out_target.str() << " vs input: " << num_2 << endl;
out_target.str("");
输出:
field: 2000000000 vs input: 2000000000
所需的输出为“20000000”。没有什么可以阻止我使用第二个操作来仅获取前 8 个字符,但是 iomanip 真的缺少字段截断吗? Boost 格式化可以一步完成我所需要的吗?
In C++ I need string representations of integers with leading zeroes, where the representation has 8 digits and no more than 8 digits, truncating digits on the right side if necessary. I thought I could do this using just ostringstream and iomanip.setw(), like this:
int num_1 = 3000;
ostringstream out_target;
out_target << setw(8) << setfill('0') << num_1;
cout << "field: " << out_target.str() << " vs input: " << num_1 << endl;
The output here is:
field: 00003000 vs input: 3000
Very nice! However if I try a bigger number, setw lets the output grow beyond 8 characters:
int num_2 = 2000000000;
ostringstream out_target;
out_target << setw(8) << setfill('0') << num_2;
cout << "field: " << out_target.str() << " vs input: " << num_2 << endl;
out_target.str("");
output:
field: 2000000000 vs input: 2000000000
The desired output is "20000000". There's nothing stopping me from using a second operation to take only the first 8 characters, but is field truncation truly missing from iomanip? Would the Boost formatting do what I need in one step?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想不出任何方法来截断这样的数字字段。也许它尚未实施,因为它会改变值。
ostream::write()
允许您足够简单地截断字符串缓冲区,如本例所示...I can't think of any way to truncate a numeric field like that. Perhaps it has not been implemented because it would change the value.
ostream::write()
allows you to truncate a string buffer simply enough, as in this example...如果您假设 snprintf() 将写入尽可能多的字符(我认为这不能保证),
我不确定为什么您希望 20 亿与 2000 万相同。在截断时发出错误信号可能更有意义,如下所示:
If you assume that snprintf() will write as many chars at it can (I don't think this is guaranteed),
I am not sure why you want 2 billion to be the same as 20 million. It may make more sense to signal an error on truncation, like this: