如何在puttime中设置宽度? [英] How can I Format Width in puttime?
本文介绍了如何在puttime中设置宽度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
1月1日
2月2日
3月3日
4月4日
5月5日
6月6日
7月7日
8月8日
9月9日
10月10日
十一月十一日
十二月十二日
我想要做到这一点:
(tm i {0,0,0,1,0}; i.tm_mon <12; ++ i.tm_mon)cout的 << put_time(& i,%-9B)< i.tm_mon + 1<< ENDL;
不幸的是 puttime
似乎不允许我在它的格式字段中使用字段标志。另外,这个 puttime
对于 setw
来说似乎没什么用处。
是我唯一的选择 strftime
,然后用 setw
?
解决方案
以下是标题只有库,尊重I / O操纵符:
#includedate.h
#include< iomanip>
#include< iostream>
int
main()
{
using namespace date;
使用namespace std;
auto m = jan;
do
{
cout<<左<< (10)<格式(%B,sys_days {m / 1/1})<<正确
<<无符号(m)<< \\\
;
} while(++ m!= jan);
$
你可以通过将上面的代码粘贴到 wandbox link 。
January 1
February 2
March 3
April 4
May 5
June 6
July 7
August 8
9月9日
10月10日
11月11日
12月12日
$ / code>
Let's say that I want to print something simple like this table:
January 1
February 2
March 3
April 4
May 5
June 6
July 7
August 8
September 9
October 10
November 11
December 12
I'd like to accomplish this like:
for(tm i{ 0, 0, 0, 1, 0 }; i.tm_mon < 12; ++i.tm_mon) cout << put_time(&i, "%-9B") << i.tm_mon + 1 << endl;
Unfortunately puttime
doesn't seem to allow me to use field flags in it's format fields. Additionally this puttime
doesn't seem to play nice with setw
.
Is my only option to do strftime
and then use that with setw
?
解决方案 Here is a header-only library that respects the I/O manipulators:
#include "date.h"
#include <iomanip>
#include <iostream>
int
main()
{
using namespace date;
using namespace std;
auto m = jan;
do
{
cout << left << setw(10) << format("%B", sys_days{m/1/1}) << right
<< unsigned(m) << '\n';
} while (++m != jan);
}
You can try this out yourself by pasting the above code into this wandbox link.
January 1
February 2
March 3
April 4
May 5
June 6
July 7
August 8
September 9
October 10
November 11
December 12
这篇关于如何在puttime中设置宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文