头文件#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++输出二维字符矩阵对齐的详细内容,
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)