简体   繁体   English

使用 C++ output 运算符打印前导零?

[英]Print leading zeros with C++ output operator?

How can I format my output in C++?如何在 C++ 中格式化我的 output? In other words, what is the C++ equivalent to the use of printf like this:换句话说,什么是 C++ 相当于使用printf像这样:

printf("%05d", zipCode);

I know I could just use printf in C++, but I would prefer the output operator << .我知道我可以在 C++ 中使用printf ,但我更喜欢 output 运算符<<

Would you just use the following?你会使用以下内容吗?

std::cout << "ZIP code: " << sprintf("%05d", zipCode) << std::endl;

This will do the trick, at least for non-negative numbers (a) such as the ZIP codes (b) mentioned in your question.这将起到作用,至少对于非负数(a),例如您的问题中提到的邮政编码(b)

#include <iostream>
#include <iomanip>

using namespace std;
cout << setw(5) << setfill('0') << zipCode << endl;

// or use this if you don't like 'using namespace std;'
std::cout << std::setw(5) << std::setfill('0') << zipCode << std::endl;

The most common IO manipulators that control padding are:控制填充的最常见的 IO 操纵器是:

  • std::setw(width) sets the width of the field. std::setw(width)设置字段的宽度。
  • std::setfill(fillchar) sets the fill character. std::setfill(fillchar)设置填充字符。
  • std::setiosflags(align) sets the alignment, where align is ios::left or ios::right. std::setiosflags(align)设置对齐方式,其中 align 为 ios::left 或 ios::right。

And just on your preference for using << , I'd strongly suggest you look into the fmt library (see https://github.com/fmtlib/fmt ).根据您对使用<<的偏好,我强烈建议您查看fmt库(请参阅https://github.com/fmtlib/fmt )。 This has been a great addition to our toolkit for formatting stuff and is much nicer than massively length stream pipelines, allowing you to do things like:这是我们用于格式化东西的工具包的一个很好的补充,并且比大规模流管道要好得多,允许您执行以下操作:

cout << fmt::format("{:05d}", zipCode);

And it's currently being targeted by LEWG toward C++20 as well, meaning it will hopefully be a base part of the language at that point (or almost certainly later if it doesn't quite sneak in).并且它目前也被 LEWG 定位为 C++20,这意味着它有望在那时成为该语言的基础部分(或者几乎可以肯定,如果它没有完全进入)。


(a) If you do need to handle negative numbers, you can use std::internal as follows: (a)如果你确实需要处理负数,你可以使用std::internal如下:

cout << internal << setw(5) << setfill('0') << zipCode << endl;

This places the fill character between the sign and the magnitude.这地方的符号和大小之间的填充字符。


(b) This ("all ZIP codes are non-negative") is an assumption on my part but a reasonably safe one, I'd warrant :-) (b)这(“所有邮政编码都是非负的”)是我的一个假设,但一个相当安全的假设,我保证:-)

使用setw 和 setfill调用:

std::cout << std::setw(5) << std::setfill('0') << zipCode << std::endl;

In C++20 you'll be able to do:在 C++20 中,您将能够执行以下操作:

std::cout << std::format("{:05}", zipCode);

In the meantime you can use the {fmt} library , std::format is based on.同时,您可以使用{fmt} 库std::format基于。

Disclaimer : I'm the author of {fmt} and C++20 std::format .免责声明:我是 {fmt} 和 C++20 std::format

or,或者,

char t[32];
sprintf_s(t, "%05d", 1);

will output 00001 as the OP already wanted to do将输出 00001,因为 OP 已经想做

Simple answer but it works!简单的答案,但它的工作原理!

ostream &operator<<(ostream &os, const Clock &c){
// format the output - if single digit, then needs to be padded with a 0
int hours = c.getHour();

// if hour is 1 digit, then pad with a 0, otherwise just print the hour
(hours < 10) ? os << '0' << hours : os << hours;

return os; // return the stream
}

I'm using a ternary operator but it can be translated into an if/else statement as follows我正在使用三元运算符,但可以将其转换为 if/else 语句,如下所示

if(c.hour < 10){
 os << '0' << hours;
}
 else{
  os  << hours;
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM