C++
#ifndef PRO1_PAIRS_H
#define PRO1_PAIRS_H
#include <iostream>
#include <string>
using namespace std;
template <class T1, class T2>
class Pair {
private:
T1 a;
T2 b;
public:
T1 & first();
T2 & second();
T1 first() const { return a; }
T2 second() const { return b; }
Pair(const T1 & aval, const T2 & bval) : a(aval), b(bval){}
Pair(){}
};
template <class T1, class T2>
T1& Pair<T1, T2>::first() {
return a;
}
template <class T1, class T2>
T2& Pair<T1, T2>::second() {
return b;
}
#endif //PRO1_PAIRS_H
#include "pairs.h"
#include <iostream>
#include "module11_class_template/multiple_type_parameters/pairs.h"
using namespace std;
// 多个类型参数
void useMultipleTypeParameter(){
Pair<string, int> rating[4] = {
Pair<string, int>("The Purple Duke", 5),
Pair<string, int>("Jake's Frisco AI Fresco", 4),
Pair<string, int>("Mont Souffle", 5),
Pair<string, int>("Gertie's Eats", 3)
};
int joints = sizeof(rating) / sizeof(Pair<string, int>);
cout << "Rating :\t Eatery\n";
for (int i = 0; i < joints; i++) {
cout << rating[i].second() << ":\t" << rating[i].first() << endl;
cout << "Oops! Revised rating :\n";
rating[3].first() = "Gertie's Fab Eat";
rating[3].second() = 6;
cout << rating[3].second() << ":\t" << rating[3].first() << endl;
}
}
int main() {
useMultipleTypeParameter();
return 0;
}