Last active
August 29, 2015 14:19
-
-
Save Genki-S/0c96c31a214c61d02816 to your computer and use it in GitHub Desktop.
競プロで使えるC++プリントデバッグ(Operator Overloading)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <map> | |
using namespace std; | |
template<typename T1, typename T2> ostream& operator<<(ostream& s, const map<T1, T2>& m) { | |
s << "{" << endl; | |
for (typeof(m.begin()) itr = m.begin(); itr != m.end(); ++itr) { | |
s << "\t" << (*itr).first << " : " << (*itr).second << endl; | |
} | |
s << "}" << endl; | |
return s; | |
} | |
int main(int argc, char** argv) { | |
map<int, char> m; | |
for (int i = 0; i < 26; ++i) { | |
m[i] = 'a' + i; | |
} | |
cout << m << endl; | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
0 : a | |
1 : b | |
2 : c | |
3 : d | |
4 : e | |
5 : f | |
6 : g | |
7 : h | |
8 : i | |
9 : j | |
10 : k | |
11 : l | |
12 : m | |
13 : n | |
14 : o | |
15 : p | |
16 : q | |
17 : r | |
18 : s | |
19 : t | |
20 : u | |
21 : v | |
22 : w | |
23 : x | |
24 : y | |
25 : z | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
template<typename T1, typename T2> ostream& operator<<(ostream& s, const pair<T1, T2>& p) {return s << "(" << p.first << ", " << p.second << ")";} | |
int main(int argc, char** argv) { | |
pair<int, int> p = make_pair(1, 2); | |
cout << p << endl; | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(1, 2) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
using namespace std; | |
// vector | |
template<typename T> ostream& operator<<(ostream& s, const vector<T>& v) { | |
int len = v.size(); | |
for (int i = 0; i < len; ++i) { | |
s << v[i]; if (i < len - 1) s << "\t"; | |
} | |
return s; | |
} | |
// 2 dimentional vector | |
template<typename T> ostream& operator<<(ostream& s, const vector< vector<T> >& vv) { | |
int len = vv.size(); | |
for (int i = 0; i < len; ++i) { | |
s << vv[i] << endl; | |
} | |
return s; | |
} | |
int main(int argc, char** argv) { | |
int N = 10; | |
vector<int> a(N); | |
for (int i = 0; i < N; ++i) { | |
a[i] = i; | |
} | |
cout << "===== vector<int> =====" << endl; | |
cout << a << endl; | |
vector< vector<int> > b(N, vector<int>(N)); | |
for (int i = 0; i < N; ++i) { | |
for (int j = 0; j < N; ++j) { | |
b[i][j] = i * j; | |
} | |
} | |
cout << endl << "===== vector< vector<int> > =====" << endl; | |
cout << b << endl; | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
===== vector<int> ===== | |
0 1 2 3 4 5 6 7 8 9 | |
===== vector< vector<int> > ===== | |
0 0 0 0 0 0 0 0 0 0 | |
0 1 2 3 4 5 6 7 8 9 | |
0 2 4 6 8 10 12 14 16 18 | |
0 3 6 9 12 15 18 21 24 27 | |
0 4 8 12 16 20 24 28 32 36 | |
0 5 10 15 20 25 30 35 40 45 | |
0 6 12 18 24 30 36 42 48 54 | |
0 7 14 21 28 35 42 49 56 63 | |
0 8 16 24 32 40 48 56 64 72 | |
0 9 18 27 36 45 54 63 72 81 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment