Home  >  Article  >  Backend Development  >  c++ output two-dimensional character matrix alignment

c++ output two-dimensional character matrix alignment

little bottle
little bottleforward
2019-04-24 14:40:105637browse

In this article, the editor would like to remind everyone about C. The main content of this article is to use C to output two-dimensional character matrix alignment. It has certain reference value. Interested friends can learn about it.

Header file#include 3f68df5471146346142495b14e43a419

Keywords: setw(n),std::left,std::right

Example: Output a 0-4 The 12*12 square matrix requires a digital width of 4, left-aligned, and the output of the producer, time, and running time in the lower right corner aligned to the right.

Code:

#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;
}

Result:

A setw and std::right are only valid for the following variable, remember.

Related tutorials: C Video Tutorial

The above is the detailed content of c++ output two-dimensional character matrix alignment. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete