Print data of any type aligned

Viewed 100

I'm a beginner in C++. While searching for a function which could help print any data type, I have found this template:

template <typename Arg, typename... Args>
void doPrint(Arg&& arg, Args&&... args)
{
    std::stringstream out;

    out << std::forward<Arg>(arg);
    using expander = int[];
    (void)expander {
        0, (void(out << std::setw(15) << std::forward<Args>(args)), 0)...
    };

    OutputDebugStringA(out.str().c_str());
}

doPrint("TEST",
    "\nname:", "Louis", "Elizabeth", "Charles Von Mayer", "Nar",        
    "\nx:", 10000, "y:", 200000000, "w:", 300, "h:", 4000000000000000,
    "\ncolor:", "orange", "blue", "green");

I would like to request help in editing this function to print the data aligned, so it could be easier to read/understand.

At the moment, this is how the data is printed to Visual Studio's output:

TEST         
name:          Louis      ElizabethCharles Von Mayer            Nar            
x:          10000             y:      200000000             w:            300             h:4000000000000000        
color:         orange           blue          green

I was trying to format the data one under another, something like this:

Formated

TEST
name:          Louis          Elizabeth      Charles Von Mayer             Nar            x:             10000          y:            200000000      w:             300            h:             4000000000000000              color:         orange         blue           green

I tried to use setw(), but it breaks the indentation in case the strings have different lengths.

While searching for a similar question, do I need to loop between all of the values and calculate the biggest length, and then append the spaces according to that?

0 Answers
Related