1 #include <vector> 2 #include <iostream> 3 #include "printCollection.h" 4 5 using namespace std; 6 7 /** 8 * Return the maximum item in array a. 9 *Assume a.size()>0; 10 *Comparable object must provide operator< and operator= 11 */ 12 template<class comparable> 13 const comparable& findMax(const vector<comparable> &rhs) 14 { 15 int maxIndex = 0; 16 17 for(int i = 0; i < rhs.size(); i++) 18 if(rhs[maxIndex] < rhs[i] ) 19 maxIndex = i; 20 21 return rhs[maxIndex]; 22 } 23 24 int main() 25 { 26 int ia[7] = {1, 2, 23, 13, 44, 12, 231}; 27 string sa[] = {"ab", "b", "c"}; 28 29 vector<int> iva(ia, ia+sizeof(ia)/sizeof(ia[0])); 30 vector<string> sva(sa, sa+sizeof(sa)/sizeof(sa[0])); 31 32 cout << findMax(iva) << endl; 33 cout << findMax(sva) << endl; 34 }
原文地址:http://www.cnblogs.com/dracohan/p/3855368.html