1

This is a follow-up to Kerrek SB etc's previous question on pretty-printing STL containers Pretty-print C++ STL containers , for which Kerrek etc managed to develop a very elegant and fully general solution, and its sequel, Pretty-print std::tuple , where std::tuple<Args...> is handled.

I wonder, if it's possible, to inlucde in the solution about indent?

For example, if I have a vector of vector of vector of string, I'd like to see something like:

{ // vector of vector of vector of string
   { // vector of vector of string
      { hello, world };// vector of string
      { The, quick, brown, fox, jumps, over, the, lazy, dog };
      ...
      { Now, is, the, time, for, all, good, men, to, come, to, the, aid, of, the, party}
   };
   { // vector of vector of string
      { hello, world };// vector of string
      { The, quick, brown, fox, jumps, over, the, lazy, dog };
      ...
      { Now, is, the, time, for, all, good, men, to, come, to, the, aid, of, the, party}
   };
   ...
   { // vector of vector of string
      { hello, world };// vector of string
      { The, quick, brown, fox, jumps, over, the, lazy, dog };
      ...
      { Now, is, the, time, for, all, good, men, to, come, to, the, aid, of, the, party}
   };
}

How would that be done nicely?

Community
  • 1
  • 1
athos
  • 5,550
  • 3
  • 40
  • 81

1 Answers1

2

Note: This is not a 'real' answer to the question as I interpret it because I suspect that OP wants a general solution which uses proper indentation for arbitrarily nested containers.

As a workaround you could of course use the given solutions and properly adjust prefix, delimiter and postfix.

Example:

using namespace std::string_literals;

using vs = std::vector<std::string>;
using vvs = std::vector<std::vector<std::string>>;
using vvvs = std::vector<std::vector<std::vector<std::string>>>;
std::cout << pretty::decoration<vs>("       { ", ", ", " }");
std::cout << pretty::decoration<vvs>("  {\n", ",\n", "\n    }");
std::cout << pretty::decoration<vvvs>("{\n", ",\n", "\n}\n");

std::vector<std::string> vs1 = { "hello"s, "world"s };
std::vector<std::string> vs2 = { "The"s, "quick"s, "brown"s, "fox"s };
std::vector<std::vector<std::string>> vvs1 = { vs1, vs1, vs2 };
std::vector<std::vector<std::string>> vvs2 = { vs2, vs1, vs2 };
std::vector<std::vector<std::vector<std::string>>> vvvs1 = { vvs1, vvs2 };

std::cout << vvvs1;

Prints:

{
    {
        { hello, world },
        { hello, world },
        { The, quick, brown, fox }
    },
    {
        { The, quick, brown, fox },
        { hello, world },
        { The, quick, brown, fox }
    }
}
Community
  • 1
  • 1
Pixelchemist
  • 21,390
  • 6
  • 40
  • 70