It is pretty easy to print anything with output streams, as the STL is already shipped with many useful operator<<
overloads for the most basic types. This way, data structures containing items of such types can easily be printed using the std::ostream_iterator
class, which we've already done quite often in this book.
In this recipe, we will concentrate on how to do this with a custom type, and what else we can do to manipulate printing via template type choices without much code at the caller side.
We will play with std::ostream_iterator
by enabling for combination with a new custom class and have a look into its implicit conversion capabilities, which can help us with printing:
- The include files come first and then we declare that we use the
std
namespace by default:
#include <iostream> #include <vector> #include <iterator> #include <unordered_map> #include <algorithm...