The following code seems working for the built in type, but not for the pair. Can anybody help me out? Thanks.
Code:
#include <iostream>
#include <iterator>
#include <map>
using namespace std;
template<class T>
void print2darray(const int m, const int n, const T& a) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
cout << a[i][j] << " ";
}
cout << endl;
}
}
typedef pair<string, int> mypair;
ostream& operator<<(ostream& s, const mypair& p) const{
return s << "(" << p.first << ", " << p.second;
}
int main() {
int a[2][2] = { { 1, 2, }, { 3, 4 } };
double b[2][2] = { { 1.0, 2.0 }, { 3.0, 4.0 } };
string c[2][2] = { { "as", "it" }, { "mine", "your" } };
pair<string, int> d[2][2];
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
d[i][j] = make_pair(c[i][j], a[i][j]);
}
}
cout << "array a[2][2]:" << endl;
print2darray(2,2,a);
cout << "array b[2][2]:" << endl;
print2darray(2,2,b);
cout << "array c[2][2]:" << endl;
print2darray(2,2,c);
cout << "array d[2][2]:" << endl;
print2darray(2,2,d);
}