将cout格式与表格的列对齐 [英] Align cout format as table's columns
本文介绍了将cout格式与表格的列对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我很确定这是一个关于格式化的简单问题,但这是我要完成的工作:
I'm pretty sure this is a simple question in regards to formatting but here's what I want to accomplish:
我想使用cout
将数据输出到屏幕上.我想以表格格式的形式输出.我的意思是列和行应正确对齐.示例:
I want to output data onto the screen using cout
. I want to output this in the form of a table format. What I mean by this is the columns and rows should be properly aligned. Example:
Test 1
Test2 2
Iamlongverylongblah 2
Etc 1
我只关心单个行,所以现在要输出的行(不起作用)是
I am only concerned with the individual line so my line to output now (not working) is
cout << var1 << "\t\t" << var2 << endl;
哪个给了我类似的东西
Test 1
Test2 2
Iamlongverylongblah 2
Etc 1
推荐答案
#include <iostream>
#include <iomanip>
using namespace std;
int main () {
cout << setw(21) << left << "Test" << 1 << endl;
cout << setw(21) << left << "Test2" << 2 << endl;
cout << setw(21) << left << "Iamlongverylongblah" << 2 << endl;
cout << setw(21) << left << "Etc" << 1 << endl;
return 0;
}
这篇关于将cout格式与表格的列对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文