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
- 如何部分禁用 cmake C/C++ 自定义编译器检查 2021-01-01
- 将不可复制的闭包对象传递给 std::function 参数 2021-01-01
- 为什么我们不能使用“虚拟继承"?在 COM 中? 2022-01-01
- 与 CAS 的原子交换(使用 gcc 同步内置函数) 2022-01-01
- 如何更改 SysDateTimePick32 或 CDateTimeCtrl 的背景颜色? 2022-01-01
- 随机插入/删除的综合向量与链表基准 2022-01-01
- c++ STL设置差异 2022-01-01
- 提升 ASIO 流缓冲 2021-01-01
- C++:获取传递给函数的多维数组的行大小 2021-01-01
