簡體   English   中英

按級別縮進的漂亮打印C ++容器

[英]pretty-print C++ containers with indent by level

這是Kerrek SB等人先前關於漂亮打印STL容器的問題的后續文章, Pretty-print C ++ STL容器 ,Kerrek等成功地為其開發了一個非常優雅且完全通用的解決方案,以及其后續版本Pretty-print std ::元組 ,其中處理std::tuple<Args...>

我想知道是否有可能采用縮進的解決方案?

例如,如果我有一個向量,一個向量,一個向量,一個字符串向量,我想看一下類似的東西:

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

那將如何很好地完成?

注意:按我的解釋,這不是問題的“真實”答案,因為我懷疑OP想要一個通用解決方案,該解決方案對任意嵌套的容器使用適當的縮進。

作為解決方法,您當然可以使用給定的解決方案,並適當地調整前綴,定界符和后綴。

范例

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;

打印:

{
    {
        { hello, world },
        { hello, world },
        { The, quick, brown, fox }
    },
    {
        { The, quick, brown, fox },
        { hello, world },
        { The, quick, brown, fox }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM