【问题标题】:How to format this output C++如何格式化此输出 C++
【发布时间】:2016-03-25 18:01:40
【问题描述】:

我有一张地图,其中包含一个单词和一组整数作为值。 我想输出单词左对齐,然后在排列的列中输出集合中的整数值。我认为这会起作用,但它似乎输出非常糟糕。

我将如何进行调整以使数字列彼此对齐并在它们之间留出相当数量的空格?

for (auto cbegin = ident_map.begin(); cbegin != ident_map.end(); cbegin++) {
    outFile << left << (*cbegin).first << setw(10);
    for (set<int>::iterator setITR = (*cbegin).second.begin(); setITR != (*cbegin).second.end(); setITR++) {
        outFile << right << *setITR << setw(4);
    }
    outFile << endl;
}

我认为这应该正确输出,但它看起来像这样:

BinarySearchTree         4
Key          4  27
OrderedPair         1   4   8  14
T            4
erase        27
first         7  13
insert         1   4
key         27
kvpair         1   4
map_iterator         1   3   8  14
mitr         3   7   8  13  14
result         4   6   7  13
result2         8   9  14  15
second         6
t            4
value_type         1

【问题讨论】:

  • 继续试验。但是,在打印值之后设置字段宽度没有多大意义。
  • 你正在写它(除了在打印之前使用它,就像上面评论中所说的那样)。找到最好的价值

标签: c++ format iomanip


【解决方案1】:

试试Boost.Format

这是一个与您想要做的事情在精神上相似的玩具示例。 %|30t|%|50t| 格式化程序可确保您的数字分别在第 30 列和第 50 列左对齐。

#include <iostream>
#include <boost/format.hpp>

int main(int argc, char *argv[]) {
    std::cout << "0         1         2         3         4         5         " << std::endl;
    std::cout << "012345678901234567890123456789012345678901234567890123456789" << std::endl;

    for (int i = 0; i < 10; i++) {

        // Set up the format to have 3 variables with vars 2 and 3 at
        // columns 30 and 50 respectively.
        boost::format fmt("string %1%: %|30t|%2% %|50t|%3%");

        // Append values we want to print
        fmt = fmt % i;
        for (int j = 0; j < 2; j++) {
            fmt = fmt % rand();
        }

        // Write to std::cout
        std::cout << fmt << std::endl;

        // Or save as a string...
        std::string s = fmt.str();
    }
    return 0;
}

运行时产生:

$ ./a.out 
0         1         2         3         4         5         
012345678901234567890123456789012345678901234567890123456789
string 0:                     16807               282475249
string 1:                     1622650073          984943658
string 2:                     1144108930          470211272
string 3:                     101027544           1457850878
string 4:                     1458777923          2007237709
string 5:                     823564440           1115438165
string 6:                     1784484492          74243042
string 7:                     114807987           1137522503
string 8:                     1441282327          16531729
string 9:                     823378840           143542612

【讨论】:

    【解决方案2】:

    试试用\t 代替setw() 怎么样。 \t 是制表符特殊字符。这将为您的格式带来奇迹,因为您只需计算出每行第一个位置的制表符数量,然后它就像遵循您想要的格式一样简单,效果很好,因为制表符是统一的大小

    【讨论】:

    • '/t' 一旦数字不同于 0->9 或 10->99 或 100->999 等等,一切都会乱七八糟
    • 只有当数字达到大于制表符空间的大小时,它才会有所不同。 (4-8) 个字符,具体取决于实现。
    猜你喜欢
    • 2020-11-14
    • 1970-01-01
    • 1970-01-01
    • 2017-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多