gpt4 book ai didi

c++ - 按级别缩进的 pretty-print C++ 容器

转载 作者:行者123 更新时间:2023-11-30 03:35:41 24 4
gpt4 key购买 nike

这是 Kerrek SB 的后续行动etc 之前关于 pretty-print STL 容器的问题 Pretty-print C++ STL containers ,为此,Kerrek 等设法开发了一个非常优雅且完全通用的解决方案及其续集,Pretty-print std::tuple , 其中std::tuple<Args...>已处理。

我想知道,是否可以将缩进的解决方案包括在内?

例如,如果我有一个字符串 vector 的 vector vector ,我希望看到如下内容:

{ // 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 想要一个通用解决方案,该解决方案对任意嵌套的容器使用适当的缩进.

作为解决方法,您当然可以使用给定的解决方案并适当调整前缀、分隔符和后缀。

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;

打印:

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

关于c++ - 按级别缩进的 pretty-print C++ 容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41093221/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com