函式題,實作函式即可,輸入輸出已經寫好了。題目中使用的code在最下面。
定義一個類別 class Pair ,其包含的成員如下 (:)
template<class T1,class T2> class Pair{ private: T1 first; T2 second; int index; public: // Pair(); 本題不需要實作這個 Pair(T1,T2,int); ~Pair(); void print_first_and_second(); };
你需要實作三個函式。 . Pair 的 constructor : Pair(T1,T2,int); . Pair 的 destructor, : ~Pair(); . Pair 的 member function , 用來輸出first,second的值 : void print_first_and_second();
#include <iostream> #include <iomanip> using std::cin; using std::cout; using std::endl; template<typename T1,typename T2> class Pair{ private: T1 first; T2 second; int index; public: // Pair(); 本題不需要實作這個 Pair(T1,T2,int); ~Pair(); void print_first_and_second(); }; /******************************/ /* Your code will be put here */ /******************************/ int main() { /* str表示int或double */ char str[10]; cin >> str; /* N表示輸入的組數 */ int N; cin >> N; /* 兩個if都會建立Pair的物件 分別傳入 1. <int,int> 2. <double,double> 作為Pair的first和second型態的引數 E.g., typeof(first) = int , typeof(second) = int Or typeof(first) = double , typeof(second) = double */ if(!(strcmp(str,"int"))){ int first,second; for(int i=0;i<N;i++){ cin >> first >> second; Pair<int,int> p(first,second,i); p.print_first_and_second(); } } if(!(strcmp(str,"double"))){ double first,second; for(int i=0;i<N;i++){ cin >> first >> second; Pair<double,double> p(first,second,i); p.print_first_and_second(); } } return 0; }