写个简单的例子,一看就懂了,还是代码来的直接啊。
#include <iostream> #include <algorithm> using namespace std; bool myfn(int i, int j) { return i<j; } int main () { int myints[] = {3,7,2,5,6,4,9}; // using default comparison: cout << "The smallest element is " << *min_element(myints,myints+7) << endl; cout << "The largest element is " << *max_element(myints,myints+7) << endl; // using function myfn as comp: cout << "The smallest element is " << *min_element(myints,myints+7,myfn) << endl; cout << "The largest element is " << *max_element(myints,myints+7,myfn) << endl; return 0; }