Skip to content

Instantly share code, notes, and snippets.

@taksatou
Created May 28, 2011
Embed
What would you like to do?
#include <iostream>
#include <map>
#include <vector>
#include <string>
using namespace std;
template <typename T, typename U>
ostream& operator<<(ostream& os, const pair<T, U> p) {
os << '(' << p.first << ':' << p.second << ')';
return os;
}
template <typename T, typename Alloc,
template <typename _1, typename _2> class Container>
ostream& operator<<(ostream& os, const Container<T, Alloc >& p) {
typename Container<T, Alloc>::const_iterator e = p.end();
os << '[';
for (typename Container<T, Alloc>::const_iterator it = p.begin(); it != e;) {
os << *it;
os << (++it == e ? "" : ", ");
}
os << ']';
return os;
}
template <typename T, typename Compare, typename Alloc,
template <typename _1, typename _2, typename _3> class Container>
ostream& operator<<(ostream& os, const Container<T, less<T>, Alloc>& p) {
typename Container<T, Compare, Alloc>::const_iterator e = p.end();
os << '[';
for (typename Container<T, Compare, Alloc>::const_iterator it = p.begin(); it != e;) {
os << *it;
os << (++it == e ? "" : ", ");
}
os << ']';
return os;
}
template <typename T, typename Compare, typename Alloc,
template <typename _1, typename _2, typename _3> class Container>
ostream& operator<<(ostream& os, const Container<T, greater<T>, Alloc>& p) {
typename Container<T, Compare, Alloc>::const_iterator e = p.end();
os << '[';
for (typename Container<T, Compare, Alloc>::const_iterator it = p.begin(); it != e;) {
os << *it;
os << (++it == e ? "" : ", ");
}
os << ']';
return os;
}
template <typename T, typename U, typename Compare, typename Alloc,
template <typename _1, typename _2, typename _3, typename _4> class Container>
ostream& operator<<(ostream& os, const Container<T, U, Compare, Alloc>& p) {
typename Container<T, U, Compare, Alloc>::const_iterator e = p.end();
os << '[';
for (typename Container<T, U, Compare, Alloc>::const_iterator it = p.begin(); it != e; ) {
os << *it;
os << (++it == e ? "" : ", ");
}
os << ']';
return os;
}
int main() {
vector<int> ary;
map<int, vector<int> > lis1;
ary.push_back(1);
lis1.insert(make_pair(1, ary));
ary.push_back(2);
lis1.insert(make_pair(2, ary));
ary.push_back(3);
lis1.insert(make_pair(3, ary));
map<string, string> lis2;
lis2.insert(make_pair("first", "1st"));
lis2.insert(make_pair("second", "2nd"));
lis2.insert(make_pair("third", "3rd"));
cout << ary << endl;
cout << lis1 << endl;
cout << lis2 << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment