转自:http://hi.baidu.com/sjh9/blog/item/6a2a98ec0d4a622463d09f36.html
1.0 std::setw(n);
标准库中的格式化流操作符,位于头文件iomanip中(#include <iomanip>)引用就可以了....
例:
#include <iostream>
#include <iomanip>
using namespace std;
void DisplayCalendar(int cal[][7]);
int main()
{
int calendar[5][7]=
{
{1,2,3,4,5,6,7},
{8,9,10,11,12,13,14},
{15,16,17,18,19,20,21},
{22,23,24,25,26,27,28},
{29,30,31}
};
DisplayCalendar(calendar);
return 0;
}
void DisplayCalendar(int cal[][7])
{
std::cout<<"Sun Mon Tue Wed Thu Fri Sat"<<std::endl ;
for(int week=0;week<5;week++)
{
for(int day=0;day<7;day++)
{
int date=cal[week][day];
if(date)
std::cout<<std::setw(4)<<date<<' ';
}
std::cout<<std::endl ;
}
}
用了std::setw(4)运行结果如下:
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
请按任意键继续. . .
去掉以后如下:
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
请按任意键继续. . .