Home > Backend Development > C#.Net Tutorial > c++ output two-dimensional character matrix alignment

c++ output two-dimensional character matrix alignment

little bottle
Release: 2019-04-24 14:40:30
forward
5732 people have browsed it

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

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;
}
Copy after login

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!

Related labels:
c++
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template