截断 C++由 ostringstream、iomanip:setw 生成的字符串字段

发布于 2024-09-02 18:23:11 字数 922 浏览 2 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

维持三分热 2024-09-09 18:23:11

我想不出任何方法来截断这样的数字字段。也许它尚未实施,因为它会改变值。

ostream::write() 允许您足够简单地截断字符串缓冲区,如本例所示...

    int num_2 = 2000000000;
    ostringstream out_target;

    out_target << setw(8) << setfill('0') << num_2;
    cout << "field: ";
    cout.write(out_target.str().c_str(), 8);
    cout << " vs input: " << num_2 << endl;

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...

    int num_2 = 2000000000;
    ostringstream out_target;

    out_target << setw(8) << setfill('0') << num_2;
    cout << "field: ";
    cout.write(out_target.str().c_str(), 8);
    cout << " vs input: " << num_2 << endl;
蓝眼睛不忧郁 2024-09-09 18:23:11

如果您假设 snprintf() 将写入尽可能多的字符(我认为这不能保证),

char buf[9];
snprintf(buf, 10, "%08d", num);
buf[8] = 0;
cout << std::string(buf) << endl;

我不确定为什么您希望 20 亿与 2000 万相同。在截断时发出错误信号可能更有意义,如下所示:

if (snprintf(buf, 10, "%08d", num) > 8) {
    throw std::exception("oops")
}

If you assume that snprintf() will write as many chars at it can (I don't think this is guaranteed),

char buf[9];
snprintf(buf, 10, "%08d", num);
buf[8] = 0;
cout << std::string(buf) << endl;

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:

if (snprintf(buf, 10, "%08d", num) > 8) {
    throw std::exception("oops")
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文