language: C++ 4.7.2 (gcc-4.7.2)
date: 874 days 9 hours ago
link:
visibility: private
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
#include <deque>
#include <list>
#include <iostream>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
 
namespace make_sure_to_put_these_overloads_in_a_namespace {
 
// Your PrintSequence adapted to a stream instead of a string:
template<class Iter>
void PrintSequence(std::ostream &s, const char* delim,
                   Iter begin, Iter end)
{
  s << delim[0];
  int size = 0;
  if (begin != end) {
    s << *begin;
    ++size;
    while (++begin != end) {
      s << ", " << *begin;
      ++size;
    }
  }
  s << delim[1] << '<' << size << '>';
}
 
#define OUTPUT_TWO_ARG_CONTAINER(Sequence) \
template<class T1, class T2> \
std::ostream& operator<<(std::ostream &s, Sequence<T1, T2> const &seq) { \
  PrintSequence(s, "[]", seq.begin(), seq.end()); \
  return s; \
}
 
OUTPUT_TWO_ARG_CONTAINER(std::vector)
OUTPUT_TWO_ARG_CONTAINER(std::deque)
OUTPUT_TWO_ARG_CONTAINER(std::list)
// other types
#undef OUTPUT_TWO_ARG_CONTAINER
 
template<class First, class Second>
std::ostream& operator<<(std::ostream &s, std::pair<First, Second> const &p) { \
  s << "(" << p.first << ", " << p.second << ")";
  return s;
}
 
}
 
template<class T>
std::string AsString(T const &v) {
  using namespace make_sure_to_put_these_overloads_in_a_namespace;
  std::ostringstream ss;
  ss << v;
  return ss.str();
}
 
int main() {
  using namespace std;
  vector<int> v;
  v.push_back(1);
  v.push_back(2);
  std::cout << "v = " << AsString(v) << '\n';
 
  vector<vector<int> > m;
  m.push_back(v);
  m.push_back(v);
  std::cout << "m = " << AsString(m) << '\n';
 
  return 0;
}