/** \file \class Common::Cont2Cout \author Matthias Finke \date 20080405 \version 0.2 \brief streams content of an container to cout */ #ifndef CONT2COUT_HPP_ #define CONT2COUT_HPP_ #include #include #include namespace Common { template class Cont2Cout : public std::binary_function { public: /** *\param cont a container like std::vector *\param delim delimiter to be used by cout (default \n) */ void operator()(const CONT& cont, const char* delim = "\n") const { typedef typename CONT::value_type type; std::copy(cont.begin(), cont.end(), std::ostream_iterator(std::cout,delim)); } /** *\param cont a container like std::vector *\param delim delimiter to be used by cout (default \n) */ void operator()(const CONT* cont, const char* delim = "\n") const { typedef typename CONT::value_type type; std::copy(cont->begin(), cont->end(), std::ostream_iterator(std::cout,delim)); } }; template class Pair2CoutFunc : public std::unary_function { std::string const prae_; /**< \a prae_ a string, which will be put to cout before the first element of the pair */ std::string const mid_; /**< \a mid_ a string, which will be put to cout between the two elements of the pair*/ std::string const post_; /**< \a post_ a string, which will be put to cout after the second element of the pair*/ public: /** *\param prae a string, which will be put to cout before the first element of the pair *\param mida string, which will be put to cout between the two elements of the pair *\param post a string, which will be put to cout after the second element of the pair * the constructor */ Pair2CoutFunc(std::string const& prae = "(", std::string const& mid = ",", std::string const& post = ")\n") : prae_(prae), mid_(mid), post_(post) {} /** * \param datCont an object containing a pair (e.g. a std::pair object) * puts the content of a pair to cout * \note the \a datCont object must have first and second as members */ void operator()(DATCONT const& datCont) const { std::cout << prae_ << datCont.first << mid_ << datCont.second << post_; } }; template class DATCONT = std::pair > class Pair2Cout { public: /** *\param cont a reference to a container with pair-object as elements *\param prae a string, which will be put to cout before the first element of the pair *\param mida string, which will be put to cout between the two elements of the pair *\param post a string, which will be put to cout after the second element of the pair * puts the content of an container with pair-objects to cout */ void operator()(CONT const& cont, std::string const& prae = "(", std::string const& mid = ",", std::string const& post = ")\n") const { typedef typename CONT::value_type THEPAIR; std::for_each(cont.begin(), cont.end(), Pair2CoutFunc(prae,mid,post) ); } /** *\param cont a pointer to a container with pair-object as elements *\param prae a string, which will be put to cout before the first element of the pair *\param mida string, which will be put to cout between the two elements of the pair *\param post a string, which will be put to cout after the second element of the pair * puts the content of an container with pair-objects to cout */ void operator()(CONT const* cont, std::string const& prae = "(", std::string const& mid = ",", std::string const& post = ")\n") const { typedef typename CONT::value_type THEPAIR; std::for_each(cont->begin(), cont->end(), Pair2CoutFunc(prae,mid,post) ); } }; }//end namespace #endif /*CONT2COUT_HPP_*/