头文件#include <iomanip>
关键词:setw(n),std::left,std::right
实例:输出一个0-4的12*12方阵,要求数字宽度为4,居左对齐,右下角输出出品人、时间、运行时间居右对齐。
代码:
#include <iostream>
#include <ctime>
#include <iomanip>
using namespace std;
int main()
{
srand((unsigned)time(NULL));
int p[12][12] = { 0 };
for (int i = 0; i < 12; i++)
{
for (int j = 0; j < 12; j++)
{
p[i][j] = rand() % 5;
}
}
for (int i = 0; i < 12; i++)
{
for (int j = 0; j < 12; j++)
{
cout <<std::left<< setw(4) << p[i][j];
}
cout << endl;
}
time_t now = time(0);
char *t = ctime(&now);
cout << std::right << setw(45) << "出品人:会武术之白猫" << endl;
cout << std::right << setw(46) << t << endl;
cout << std::right << setw(41) << clock() / CLOCKS_PER_SEC * 1000 << "毫秒" << endl;
}
结果:
一个setw和std::right只对后边一个变量有效,切记。
相关教程:C++视频教程
以上就是c++输出二维字符矩阵对齐的详细内容,更多请关注php中文网其它相关文章!
声明:本文转载于:博客园,如有侵犯,请联系admin@php.cn删除
程序员必备接口测试调试工具:点击使用
Apipost = Postman + Swagger + Mock + Jmeter
Api设计、调试、文档、自动化测试工具
网页生成APP,用做网站的技术去做APP:立即创建
手机网站开发APP、自助封装APP、200+原生模块、2000+映射JS接口按需打包
相关文章
相关视频