写个简单的例子,一看就懂了,还是代码来的直接啊。
#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;
}
本文展示了一个使用C++标准库函数min_element和max_element来查找数组中最小和最大元素的例子,并通过自定义比较函数实现了更灵活的查找方式。
1913

被折叠的 条评论
为什么被折叠?



