2012-02-16 22:06:01 8 Comments
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
Related Questions
Sponsored Content
17 Answered Questions
23 Answered Questions
[SOLVED] What is the "-->" operator in C++?
- 2009-10-29 06:57:45
- GManNickG
- 752686 View
- 8605 Score
- 23 Answer
- Tags: c++ operators code-formatting standards-compliance
27 Answered Questions
[SOLVED] Easiest way to convert int to string in C++
- 2011-04-08 04:19:41
- Nemo
- 2739806 View
- 1493 Score
- 27 Answer
- Tags: c++ string int type-conversion
37 Answered Questions
13 Answered Questions
[SOLVED] What is the effect of extern "C" in C++?
- 2009-06-25 02:10:07
- Litherum
- 727774 View
- 1520 Score
- 13 Answer
- Tags: c++ c linkage name-mangling extern-c
1 Answered Questions
10 Answered Questions
[SOLVED] Why is reading lines from stdin much slower in C++ than Python?
- 2012-02-21 02:17:50
- JJC
- 246532 View
- 1746 Score
- 10 Answer
- Tags: python c++ benchmarking iostream getline
7 Answered Questions
[SOLVED] What are the basic rules and idioms for operator overloading?
- 2010-12-12 12:44:56
- sbi
- 885612 View
- 2079 Score
- 7 Answer
- Tags: c++ operators operator-overloading c++-faq
4 comments
@Sebastian Mach 2012-02-21 16:26:54
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>
.@Dietmar Kühl 2012-02-16 23:47:49
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.
@celtschk 2012-02-16 22:10:18
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.@ildjarn 2012-02-16 22:11:17
Type
is calledcontainer_type::value_type
.@celtschk 2012-02-16 22:15:05
@ildjarn: But then you'll still have to specify
container_type
which, unless you are inside a template where the container type is dependend (but then it'stypename container_type::value_type
), will most likely containType
anyway. However, in C++11 you can writedecltype(cont)::value_type
(again, possibly withtypename
in templates).@mikithskegg 2012-02-16 22:12:43
In C++11 you can use range-based
for
: