-
Notifications
You must be signed in to change notification settings - Fork 9
/
templateio.hpp
82 lines (67 loc) · 2.42 KB
/
templateio.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//----------------------------------------------------------------------------
// copyright 2012, 2013, 2014 Keean Schupke
// compile with -std=c++11
// templateio.h
#ifndef TEMPLATEIO_HPP
#define TEMPLATEIO_HPP
#include <iostream>
#include <vector>
#include <tuple>
#include <map>
using namespace std;
//----------------------------------------------------------------------------
// iostream output for tuples.
template <size_t> struct idx {};
template <typename T, size_t I> ostream& print_tuple(ostream& out, T const& t, idx<I>) {
out << get<tuple_size<T>::value - I>(t) << ", ";
return print_tuple(out, t, idx<I - 1>());
}
template <typename T> ostream& print_tuple(ostream& out, T const& t, idx<1>) {
return out << get<tuple_size<T>::value - 1>(t);
}
template <typename... T> ostream& operator<< (ostream& out, tuple<T...> const& t) {
out << '<';
print_tuple(out, t, idx<sizeof...(T)>());
return out << '>';
}
template <typename S, typename T> ostream& operator<< (ostream& out, pair<S, T> const& p) {
return out << '(' << p.first << ", " << p.second << ')';
}
//----------------------------------------------------------------------------
// iostream output for vectors.
template <typename T> ostream& operator<< (ostream& out, vector<T> const& v) {
out << "[";
for (typename vector<T>::const_iterator i = v.begin(); i != v.end(); ++i) {
cout << *i;
typename vector<T>::const_iterator j = i;
if (++j != v.end()) {
cout << ", ";
}
}
return out << "]";
}
//----------------------------------------------------------------------------
// iostream output for maps.
template <typename S, typename T> ostream& operator<< (ostream& out, map<S, T> const& m) {
out << "{";
for (typename map<S, T>::const_iterator i = m.begin(); i != m.end(); ++i) {
cout << i->first << " = " << i->second;
typename map<S, T>::const_iterator j = i;
if (++j != m.end()) {
cout << ", ";
}
}
return out << "}";
}
template <typename S, typename T> ostream& operator<< (ostream& out, multimap<S, T> const& m) {
out << "{";
for (typename multimap<S, T>::const_iterator i = m.begin(); i != m.end(); ++i) {
cout << i->first << " = " << i->second;
typename multimap<S, T>::const_iterator j = i;
if (++j != m.end()) {
cout << ", ";
}
}
return out << "}";
}
#endif // TEMPLATEIO_HPP