假设我想打印像这张桌子一样简单的东西:
January 1
February 2
March 3
April 4
May 5
June 6
July 7
August 8
September 9
October 10
November 11
December 12
我想这样做:
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;
不幸的是puttime
似乎不允许我在其格式字段中使用字段标志。此外,此puttime
似乎与setw
无关。
我唯一的选择是strftime
,然后将其用于setw
?
答案 0 :(得分:1)
The following will also work
for(tm i{ 0, 0, 0, 1, 0 }; i.tm_mon < 12; ++i.tm_mon)
{
std::stringstream oss;
oss << std::put_time(&i, "%B");
string str = oss.str();
cout << std::setiosflags(std::ios::left) << setw( 10 ) << str << setw( 2 ) << i.tm_mon + 1 << endl;
}
答案 1 :(得分:1)
这是一个尊重I / O操纵器的header-only library:
#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);
}
您可以将上述代码粘贴到此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