一、使用一般的函数调用方法,代码如下:
#include <vector>
#include <string>
#include <iostream>
#include <cstring>
using namespace std;
template<typename Object, typename Comparator>
const Object &findMax(const vector<Object> &arr, Comparator cmp)
{
int maxIndex = 0;
for(int i = 1; i < arr.size(); ++i)
if(cmp.isLessThan(arr[maxIndex], arr[i]))
maxIndex = i;
return arr[maxIndex];
}
class CaseInsensitiveCompare
{
public:
bool isLessThan(const string &lhs, const string &rhs) const
{
return stricmp(lhs.c_str(), rhs.c_str()) < 0;
}
};
int main()
{
vector<string> arr(3);
arr[0] = "ZEBRA";
arr[1] = "alligator";
arr[2] = "crocodile";
cout << findMax(arr, CaseInsensitiveCompare()) << endl;
return 0;
}
二、采用函数调用操作符的方式,代码如下:
#include <vector>
#include <string>
#include <iostream>
#include <cstring>
using namespace std;
template<typename Object, typename Comparator>
const Object &findMax(const vector<Object> &arr, Comparator isLessThan)
{
int maxIndex = 0;
for(int i = 1; i < arr.size(); ++i)
if(isLessThan(arr[maxIndex], arr[i]))
maxIndex = i;
return arr[maxIndex];
}
#include <functional>
template<typename Object>
const Object &findMax(const vector<Object> &arr)
{
return findMax(arr, less<Object>());
}
class CaseInsensitiveCompare
{
public:
bool operator()(const string &lhs, const string &rhs) const
{
return stricmp(lhs.c_str(), rhs.c_str()) < 0;
}
};
int main()
{
vector<string> arr(3);
arr[0] = "ZEBRA";
arr[1] = "alligator";
arr[2] = "crocodile";
cout << findMax(arr, CaseInsensitiveCompare()) << endl;
cout << findMax(arr) << endl;
return 0;
}