c++ - 格式化 C++ 控制台输出

标签 c++ formatting

我一直在尝试将输出格式化到控制台的最长时间,但实际上并没有发生任何事情。我一直在尝试尽可能多地使用 iomanipofstream& out 函数。

void list::displayByName(ostream& out) const
{
    node *current_node  = headByName;

    // I have these outside the loop so I don't write it every time.

    out << "Name\t\t" << "\tLocation" << "\tRating " << "Acre" << endl;
    out << "----\t\t" << "\t--------" << "\t------ " << "----" << endl;

    while (current_node)
    {
        out << current_node->item.getName() // Equivalent tabs don't work?
            << current_node->item.getLocation()
            << current_node->item.getAcres()
            << current_node->item.getRating()
            << endl;

        current_node = current_node->nextByName;
    }

    // The equivalent tabs do not work because I am writing names,
    // each of different length to the console. That explains why they
    // are not all evenly spaced apart.
}

我可以使用它们来使它们彼此正确对齐吗? 我调用的函数是不言自明的,并且长度不同,因此彼此之间不能很好地对齐。

我已经尝试了 iomanip 中的所有内容。

最佳答案

把它想象成使用 Microsoft Excel :) 您将流视为字段。因此,您首先设置字段的宽度,然后在该字段中插入文本。例如:

#include <iostream>
#include <iomanip>
#include <string>

int main()
{
    using namespace std;

    string firstName = "firstName",
            secondName = "SecondName",
            n = "Just stupid Text";
    size_t fieldWidth = n.size(); // length of longest text

    cout << setw(fieldWidth) << left << firstName << endl // left padding
         << setw(fieldWidth) << left << secondName << endl
         << setw(fieldWidth) << left << n << endl;

    cout << setw(fieldWidth) << right << firstName << endl // right padding
         << setw(fieldWidth) << right << secondName << endl
         << setw(fieldWidth) << right << n << endl;
}

......

alt text

......

字段宽度仅表示文本+空格的宽度。您可以填充空格以外的任何内容:

string name = "My first name";
cout << setfill('_') << setw(name.size() + 10) << left << name;

.....

output::
My first name__________

......

我认为最好的方法是弄清楚你的格式,然后编写一个新的格式化程序来满足你的所有需求:

#include <iostream>
#include <iomanip>
#include <string>

std::ostream& field(std::ostream& o)
{
    // usually the console is 80-character wide.
    // divide the line into four fields.
    return o << std::setw(20) << std::right;
}

int main()
{
    using namespace std;

    string firstName = "firstName",
            secondName = "SecondName",
            n = "Just stupid Text";
    size_t fieldWidth = n.size();

    cout << field << firstName << endl
         << field << secondName << endl
         << field << n << endl;
}

如果你开始考虑参数化操纵器,只有接受一个 intlong 参数的很容易实现,如果你不熟悉流,其他类型真的很模糊在 C++ 中。

关于c++ - 格式化 C++ 控制台输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1449818/

相关文章:

c++ - 寻找 OpenGL 齿轮示例的 C++ 实现

c++ - 删除保留字而不在循环的第一遍调用析构函数

python - 修复 Python 中的错误?

powershell - 如何更改 Powershell 的默认输出格式以使用 Format-Table -autosize?

xaml - 有没有办法在 XAML 中对 TextBlock 进行投影效果?

c++ - sizeof 函数在函数中不起作用

c++ - 作为守护进程运行提升线程

c# - 如何将百分比字符串转换为双倍?

python - 我应该如何在 python 中缩进三元条件运算符以使其符合 PEP8?

c++ - O(NlogN) 算法运行速度比 O(n) 快...等等,什么?