MyraMath
stlprint.h
Go to the documentation of this file.
1 // ========================================================================= //
2 // This file is part of MyraMath, copyright (c) 2014-2019 by Ryan A Chilton //
3 // and distributed by MyraCore, LLC. See LICENSE.txt for license terms. //
4 // ========================================================================= //
5 
6 #ifndef MYRAMATH_UTILITY_STLPRINT_H
7 #define MYRAMATH_UTILITY_STLPRINT_H
8 
14 // Stream backends.
15 #include <iostream>
16 #include <sstream>
17 
18 // Various std::containers that are pretty-printable.
19 #include <vector>
20 #include <list>
21 #include <deque>
22 #include <set>
23 #include <map>
24 
25 // Various C++11 std::containers that are pretty-printable.
26 #ifdef MYRAMATH_ENABLE_CPP11
27 #include <array>
28 #include <unordered_set>
29 #include <unordered_map>
30 #endif
31 
32 namespace myra_stlprint {
33 
35 template<class T> std::ostream& operator << (std::ostream& out, const std::vector<T>& v)
36  {
37  out << "[ ";
38  for (typename std::vector<T>::const_iterator i = v.begin(); i != v.end(); ++i)
39  out << *i << " ";
40  out << "] (" << v.size() << ")";
41  // out << "]";
42  return out;
43  }
44 
46 template<class T> std::ostream& operator << (std::ostream& out, const std::list<T>& l)
47  {
48  out << "[ ";
49  for (typename std::list<T>::const_iterator i = l.begin(); i != l.end(); ++i)
50  out << *i << " ";
51  out << "]";
52  return out;
53  }
54 
56 template<class T> std::ostream& operator << (std::ostream& out, const std::deque<T>& v)
57  {
58  out << "[ ";
59  for (typename std::deque<T>::const_iterator i = v.begin(); i != v.end(); ++i)
60  out << *i << " ";
61  out << "] (" << v.size() << ")";
62  // out << "]";
63  return out;
64  }
65 
67 template<class First, class Second> std::ostream& operator << (std::ostream& out, const std::pair<First,Second>& p)
68  {
69  out << "{" << p.first << "," << p.second << "}";
70  return out;
71  }
72 
74 template<class T, class Comparator> std::ostream& operator << (std::ostream& out, const std::set<T,Comparator>& s)
75  {
76  out << "[ ";
77  for (typename std::set<T>::const_iterator i = s.begin(); i != s.end(); ++i)
78  out << *i << " ";
79  out << "]";
80  return out;
81  }
83 template<class T, class Comparator> std::ostream& operator << (std::ostream& out, const std::multiset<T,Comparator>& s)
84  {
85  out << "[ ";
86  for (typename std::multiset<T>::const_iterator i = s.begin(); i != s.end(); ++i)
87  out << *i << " ";
88  out << "]";
89  return out;
90  }
91 
93 template<class Key, class Value, class Comparator> std::ostream& operator << (std::ostream& out, const std::map<Key,Value,Comparator>& m)
94  {
95  out << "[ ";
96  for (typename std::map<Key,Value,Comparator>::const_iterator i = m.begin(); i != m.end(); ++i)
97  out << *i << " ";
98  out << "]";
99  return out;
100  }
101 
103 template<class Key, class Value, class Comparator> std::ostream& operator << (std::ostream& out, const std::multimap<Key,Value,Comparator>& m)
104  {
105  out << "[ ";
106  for (typename std::multimap<Key,Value,Comparator>::const_iterator i = m.begin(); i != m.end(); ++i)
107  out << *i << " ";
108  out << "]";
109  return out;
110  }
111 
112 #ifdef MYRAMATH_ENABLE_CPP11
113 
115 template<class T, size_t N> std::ostream& operator << (std::ostream& out, const std::array<T,N>& a)
116  {
117  out << "[ ";
118  for (auto iterator = a.begin(); iterator != a.end(); ++iterator)
119  out << *iterator << " ";
120  out << "]";
121  return out;
122  }
123 
125 template<class Key, class Value> std::ostream& operator << (std::ostream& out, const std::unordered_multimap<Key,Value>& m)
126  {
127  out << "[ ";
128  for (auto iterator = m.begin(); iterator != m.end(); ++iterator)
129  out << *iterator << " ";
130  out << "]";
131  return out;
132  }
133 
134 
136 template<class T> std::ostream& operator << (std::ostream& out, const std::unordered_set<T>& s)
137  {
138  out << "[ ";
139  for (auto iterator = s.begin(); iterator != s.end(); ++iterator)
140  out << *iterator << " ";
141  out << "]";
142  return out;
143  }
144 
146 template<class Key, class Value> std::ostream& operator << (std::ostream& out, const std::unordered_map<Key,Value>& m)
147  {
148  out << "[ ";
149  for (auto iterator = m.begin(); iterator != m.end(); ++iterator)
150  out << *iterator << " ";
151  out << "]";
152  return out;
153  }
154 
155 #endif
156 
158 template<class T> std::string stream2string(const T& t)
159  {
160  std::stringstream out;
161  out << t;
162  return out.str();
163  }
164 
165 } // namespace myra
166 
167 #endif
Definition: stlprint.h:32
std::string stream2string(const T &t)
Uses a std::stringstream to serialize a T into a std::string, note T must have a "std::ostream << t" ...
Definition: stlprint.h:158