函数模板求学生中成绩最高的一名
#include <iostream> #include<bits/stdc++.h> using namespace std; template<class T1,class T2> class student{ private: T1 id; T2 score; string name; public: student(string n,T1 i,T2 s){ name=n;id=i;score=s; } bool operator<(student s){ if(score<s.score) return true; else return false; } void display() { cout << "Name: " << name << endl; cout << "ID: " << id << endl; cout << "Score: " << score << endl; } }; template<class T> T getmax(T a[],int size) { T max1=a[0]; for(int i=1;i<size;i++) { if(max1<a[i]) max1=a[i]; } return max1; } int main() { student<int,int> arr[] = { student<int,int>("wyl",20223189,90), student<int,int>("wy",20223229,80), student<int,int>("mjj",20223877,91), student<int,int>("wxy",20223889,98) }; student<int,int> st = getmax(arr,4); st.display(); return 0; }
标签:26,string,int,arr,student,2023,id From: https://www.cnblogs.com/wangxinyuan1108/p/17435936.html