Align cout format as table#39;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 <<" " <<var2 <
这给了我类似的东西:
Test 1
Test2 2
Iamlongverylongblah 2
Etc 1
推荐答案
setw.
#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 格式对齐为表格的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将 cout 格式对齐为表格的列
基础教程推荐
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- CString 到 char* 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
