本文摘自PHP中文网,作者little bottle,侵删。
本篇文章,小编想带大家回忆一下C++,本篇文章的主要内容是用c++输出二维字符矩阵对齐,具有一定的参考价值,感兴趣的朋友可以了解一下。头文件#include <iomanip>
关键词:setw(n),std::left,std::right
实例:输出一个0-4的12*12方阵,要求数字宽度为4,居左对齐,右下角输出出品人、时间、运行时间居右对齐。
代码:
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 | #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++输出二维字符矩阵对齐的详细内容!
相关阅读 >>
在C++中对象如何作为参数传递和返回?(代码示例)
C++总结:面向对象的基本概念
关于C++中string类对象的用法总结
c#调用C++ 动态链接库dll
C++用什么软件编程
C++ 引用和指针区别
C++基础知识
C++学习路线
C++中string的用法介绍
const在C++中的意思
更多相关阅读请进入《C++》频道 >>
转载请注明出处:木庄网络博客 » c++输出二维字符矩阵对齐