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 格式对齐为表格的列


- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- 如何对自定义类的向量使用std::find()? 2022-11-07
- 从python回调到c++的选项 2022-11-16
- 静态初始化顺序失败 2022-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- 近似搜索的工作原理 2021-01-01
- C++ 协变模板 2021-01-01
- Stroustrup 的 Simple_window.h 2022-01-01
- STL 中有 dereference_iterator 吗? 2022-01-01