By Abhishek Anand

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


@Sebastian Mach 2012-02-21 16:26:54

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 << .

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:

template <typename C>
std::string format(C const& c) {
    std::ostringstream out;
    out << "[";
    if (!c.empty()) {
        std::copy(c.begin(), --c.end(),
            std::ostream_iterator<typename C::value_type>(out, ", "));
            out << c.back();
    out << "]";
    return out.str();

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

std::copy(cont.begin(), cont.end(),
          std::ostream_iterator<Type>(std::cout, " "));

where Type is the type of the elements of cont (e.g. if cont is of type std::vector<int> then Type must be int).

Of course instead of std::cout you can use any ostream.

@ildjarn 2012-02-16 22:11:17

Type is called container_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's typename container_type::value_type), will most likely contain Type anyway. However, in C++11 you can write decltype(cont)::value_type (again, possibly with typename in templates).

@mikithskegg 2012-02-16 22:12:43

In C++11 you can use range-based for:

for (auto& i: container)  cout << i << "  ";
cout << endl;

Related Questions

Sponsored Content

25 Answered Questions

[SOLVED] What is the "-->" operator in C++?

10 Answered Questions

15 Answered Questions

[SOLVED] What is the effect of extern "C" in C++?

1 Answered Questions

[SOLVED] The Definitive C++ Book Guide and List

  • 2008-12-23 05:23:56
  • grepsedawk
  • 2384637 View
  • 4243 Score
  • 1 Answer
  • Tags:   c++ c++-faq

28 Answered Questions

[SOLVED] Easiest way to convert int to string in C++

40 Answered Questions

19 Answered Questions

[SOLVED] How can I profile C++ code running on Linux?

  • 2008-12-17 20:29:24
  • Gabriel Isenberg
  • 528444 View
  • 1831 Score
  • 19 Answer
  • Tags:   c++ linux profiling

14 Answered Questions

[SOLVED] Why does the C++ STL not provide any "tree" containers?

  • 2008-10-15 18:52:42
  • Roddy
  • 202632 View
  • 376 Score
  • 14 Answer
  • Tags:   c++ stl tree

7 Answered Questions

13 Answered Questions

[SOLVED] Why is the C++ STL is so heavily based on templates? (and not on *interfaces*)

Sponsored Content