我希望我可以使用 cout << 打印集合/矢量/地图的内容。对于 stl 设计者来说,实现起来似乎并不困难:假设 <<被定义为T,<<对于容器来说,只需迭代元素并使用 ofstream << 打印它们即可。 。
有没有一种我不知道的简单方法可以打印它们?
如果没有,有简单的解决方案吗?我在一些地方读到扩展 stl 类是一个坏主意。是这样吗,为什么?
定义一个类似重载打印函数的东西怎么样?
编辑:
我正在寻找一个递归函数,它可以处理容器的容器......
我同意不同的人会喜欢不同的格式,但可重写的总比没有好
I wish I could just print contents of a set/vector/map by using cout << . It doesn't seem so difficult for the stl designers to implement : Assuming that << is defined for T, << for a container could just iterate through the elements and print them using ofstream << .
Is there an easy way to print them that I dont know of?
If not, Is there an easy solution? I have read at places that extending stl classes is a bad idea. Is that so, and why?
how about defining an something like an overloaded print function?
EDIT:
I am looking for a recursive function which can handle containers of containers of ...
I agree that different people would like different formats, but something overridable is better than nothing
发布评论
评论(6)
输出 STL 容器的最简单方法可能是
其中
Type
是cont
元素的类型(例如,如果cont
的类型为>std::vector
则Type
必须为int
)。当然,您可以使用任何 ostream,而不是
std::cout
。Probably the easiest way to output an STL container is
where
Type
is the type of the elements ofcont
(e.g. ifcont
is of typestd::vector<int>
thenType
must beint
).Of course instead of
std::cout
you can use any ostream.转储容器最简单的方法可能就是使用 std::copy() 。例如,我通常使用这样的东西:
是的,这并不总是有效,但可以满足我的需求。这实际上说明了标准库中没有容器输出的问题之一:容器的格式化方式有很多种。更糟糕的是,格式化输出应该是可读的,这样事情才变得真正有趣。所有这些都是可行的,但我不知道相应的建议。
The easiest eay to dump a container is probably just using
std::copy()
. For example I typically use something like this:Yes, this doesn't always work but works for my needs. This actually shows one of the problems why there is no output for containers in the standard library: there are many different ways how containers can be formatted. To make matters worse, the formatted output should be readable where thing become real fun. All of this is doable but I'm not aware of a corresponding proposal.
在 C++11 中,您可以使用基于范围的
for
:In C++11 you can use range-based
for
:使用 Template 模板参数 可以让一切变得简单,为了使其适用于每个集合,您需要 template 和 template class X 和 class...Args 作为模板参数:
输出:
Using Template template parameter makes it easy, in order to make it working for each collection you need both template<class, class...> class X and class... Args as template parameters:
output:
当然,这对他们来说并不难。但是,问问自己:输出的格式对每个客户端都有意义吗?标准库是关于重用和通用性的。将容器与一些任意的输出格式化规则耦合使得它们不那么通用,只是为了某些目的。
因此,推荐的解决方案是提供您自己的运算符 <<(std::ostream &, T) 和/或采用其他通用算法,如
中所示。
。Of course it is not hard for them. However, ask yourself: Does the format of the output make sense for every client? The standard library is about reuse and genericity. Coupling containers with some arbitrary output formatting rules makes them less generic for the sake of only some.
The recommended solution therefore is to provide your own
operator<<(std::ostream &, T)
and/or to take other generic algorithms, as found in e.g.<algorithms>
.你在寻找这样的东西吗?
然后你就可以这样使用它:
Are you looking something like this?
Then you just may use it this way: