Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • C++

Idiom #243 Print list

Print the contents of the list or array a on the standard output.

#include <algorithm>
#include <iostream>
#include <iterator>
std::copy(a.begin(), a.end(), std::ostream_iterator<int>(std::cout, "\n"));

a can be any container that supplies iterators

The template parameter type on std::ostream_iterator should match the value_type on a.

Prints each element on a separate line; you can change the delimiter between elements by replacing "\n", BUT be warned: the delimiter will appear after the last element as well
(print a)

New implementation...