1

I've got a map containing a word and a set of integers as the value. I want to output the word left aligned and then the integer values in the set in columns that are lined up. I thought that this would work but it seems to output very badly.

How would I go about adjusting this so that the number columns line up with one another and have a decent amount of spaces in between them?

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;
}

I think that this should output correctly but it comes out looking like this:

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
monkeys8
  • 11
  • 1

2 Answers2

0

Try Boost.Format

Here is a toy example that is similar in spirit to what you want to do. The %|30t| and %|50t| formatters ensure that your numbers are left-justified at the columns 30 and 50 respectively.

#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;
}

Which when run, produces:

$ ./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
lobudge
  • 171
  • 4
-1

How about try using \t instead of setw(). \t is the tab special character. This will do wonders for your formatting as you can just figure out the number of tabs to your first position for each line, then it is as simple as following what format you would like, works great because tab is a uniform size

Cmoraski
  • 78
  • 4