Changeset d3f9ad


Ignore:
Timestamp:
Jun 26, 2013, 2:19:11 PM (12 years ago)
Author:
Frederik Heber <heber@…>
Children:
df5994
Parents:
73e948
git-author:
Frederik Heber <heber@…> (06/26/13 14:17:39)
git-committer:
Frederik Heber <heber@…> (06/26/13 14:19:11)
Message:

Enhanced toString header to allow output of all STL containers.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/CodePatterns/toString.hpp

    r73e948 rd3f9ad  
    1919#include <sstream>
    2020
     21#include <deque>
    2122#include <list>
    2223#include <map>
     24#include <queue>
    2325#include <set>
    2426#include <vector>
    2527
    2628
     29/** Operator for output to std::ostream operator of an std::deque of arbitrary
     30 * type.
     31 * @param ost output stream
     32 * @param _deque deque of types to output
     33 * @return ost output stream
     34 */
     35template <class T, class alloc> std::ostream & operator<<(std::ostream &ost, const std::deque< T, alloc > &_deque)
     36{
     37  ost << "( ";
     38  for (typename std::deque< T, alloc >::const_iterator iter = _deque.begin();
     39      iter != _deque.end();
     40      ++iter)
     41    ost << *iter << "; ";
     42  ost << ")";
     43  return ost;
     44}
     45
     46
    2747/** Operator for output to std::ostream operator of an std::list of arbitrary
    2848 * type.
    2949 * @param ost output stream
    30  * @param vector list of types to output
    31  * @return ost output stream
    32  */
    33 template <class T> std::ostream & operator<<(std::ostream &ost, const std::list< T > &_vector)
    34 {
    35   ost << "( ";
    36   for (typename std::list< T >::const_iterator iter = _vector.begin();
    37       iter != _vector.end();
     50 * @param _list list of types to output
     51 * @return ost output stream
     52 */
     53template <class T, class alloc> std::ostream & operator<<(std::ostream &ost, const std::list< T, alloc > &_list)
     54{
     55  ost << "( ";
     56  for (typename std::list< T, alloc >::const_iterator iter = _list.begin();
     57      iter != _list.end();
    3858      ++iter)
    3959    ost << *iter << "; ";
     
    4969 * @return ost output stream
    5070 */
    51 template <class T, class S> std::ostream & operator<<(std::ostream &ost, const std::map< T, S > &_vector)
     71template <class T, class S, class comp, class alloc> std::ostream & operator<<(std::ostream &ost, const std::map< T, S, comp, alloc > &_map)
    5272{
    5373  ost << "{ ";
    54   for (typename std::map< T, S >::const_iterator iter = _vector.begin();
    55       iter != _vector.end();
     74  for (typename std::map< T, S, comp, alloc >::const_iterator iter = _map.begin();
     75      iter != _map.end();
    5676      ++iter)
    5777    ost << "(" << iter->first << ")=>(" << iter->second << "); ";
     
    6080}
    6181
    62 
    6382/** Operator for output to std::ostream operator of an std::multimap of arbitrary
    6483 * type.
     
    6786 * @return ost output stream
    6887 */
    69 template <class T, class S> std::ostream & operator<<(std::ostream &ost, const std::multimap< T, S > &_vector)
     88template <class T, class S, class comp, class alloc> std::ostream & operator<<(std::ostream &ost, const std::multimap< T, S, comp, alloc > &_multimap)
    7089{
    7190  ost << "{ ";
    72   for (typename std::multimap< T, S >::const_iterator iter = _vector.begin();
    73       iter != _vector.end();
     91  for (typename std::multimap< T, S, comp, alloc >::const_iterator iter = _multimap.begin();
     92      iter != _multimap.end();
    7493      ++iter)
    7594    ost << "(" << iter->first << ")=>(" << iter->second << "); ";
     
    7897}
    7998
     99/** Operator for output to std::ostream operator of an std::multiset of arbitrary
     100 * type.
     101 * @param ost output stream
     102 * @param multiset list of types to output
     103 * @return ost output stream
     104 */
     105template <class T, class comp, class alloc> std::ostream & operator<<(std::ostream &ost, const std::multiset< T, comp, alloc > &_multiset)
     106{
     107  ost << "( ";
     108  for (typename std::multiset< T, comp, alloc >::const_iterator iter = _multiset.begin();
     109      iter != _multiset.end();
     110      ++iter)
     111    ost << *iter << "; ";
     112  ost << ")";
     113  return ost;
     114}
     115
     116/** Operator for output to std::ostream operator of an std::pair of arbitrary
     117 * type.
     118 * @param ost output stream
     119 * @param _pair pair of types to output
     120 * @return ost output stream
     121 */
     122template <class T, class S> std::ostream & operator<<(std::ostream &ost, const std::pair< T, S > &_pair)
     123{
     124  ost << "[" << _pair.first << "," << _pair.second << "]";
     125  return ost;
     126}
     127
     128/** Operator for output to std::ostream operator of an std::priority_queue of arbitrary
     129 * type.
     130 * @param ost output stream
     131 * @param _priority_queue queue of types to output
     132 * @return ost output stream
     133 */
     134template <class T, class alloc> std::ostream & operator<<(std::ostream &ost, const std::priority_queue< T, alloc > &_priority_queue)
     135{
     136  ost << "( ";
     137  for (typename std::priority_queue< T, alloc >::const_iterator iter = _priority_queue.begin();
     138      iter != _priority_queue.end();
     139      ++iter)
     140    ost << *iter << "; ";
     141  ost << ")";
     142  return ost;
     143}
     144
     145/** Operator for output to std::ostream operator of an std::queue of arbitrary
     146 * type.
     147 * @param ost output stream
     148 * @param _queue queue of types to output
     149 * @return ost output stream
     150 */
     151template <class T, class alloc> std::ostream & operator<<(std::ostream &ost, const std::queue< T, alloc > &_queue)
     152{
     153  ost << "( ";
     154  for (typename std::queue< T, alloc >::const_iterator iter = _queue.begin();
     155      iter != _queue.end();
     156      ++iter)
     157    ost << *iter << "; ";
     158  ost << ")";
     159  return ost;
     160}
     161
    80162
    81163/** Operator for output to std::ostream operator of an std::set of arbitrary
     
    85167 * @return ost output stream
    86168 */
    87 template <class T> std::ostream & operator<<(std::ostream &ost, const std::set< T > &_vector)
    88 {
    89   ost << "( ";
    90   for (typename std::set< T >::const_iterator iter = _vector.begin();
    91       iter != _vector.end();
    92       ++iter)
    93     ost << *iter << "; ";
    94   ost << ")";
    95   return ost;
    96 }
    97 
     169template <class T, class comp, class alloc> std::ostream & operator<<(std::ostream &ost, const std::set< T, comp, alloc > &_set)
     170{
     171  ost << "( ";
     172  for (typename std::set< T, comp, alloc >::const_iterator iter = _set.begin();
     173      iter != _set.end();
     174      ++iter)
     175    ost << *iter << "; ";
     176  ost << ")";
     177  return ost;
     178}
    98179
    99180/** Operator for output to std::ostream operator of an std::vector of arbitrary
     
    103184 * @return ost output stream
    104185 */
    105 template <class T> std::ostream & operator<<(std::ostream &ost, const std::vector< T > &_vector)
    106 {
    107   ost << "( ";
    108   for (typename std::vector< T >::const_iterator iter = _vector.begin();
     186template <class T, class alloc> std::ostream & operator<<(std::ostream &ost, const std::vector< T, alloc > &_vector)
     187{
     188  ost << "( ";
     189  for (typename std::vector< T, alloc >::const_iterator iter = _vector.begin();
    109190      iter != _vector.end();
    110191      ++iter)
Note: See TracChangeset for help on using the changeset viewer.