现在的位置: 首页 > 综合 > 正文

取容器中的最大最小值min_element(),max_element()

2013年03月13日 ⁄ 综合 ⁄ 共 480字 ⁄ 字号 评论关闭

写个简单的例子,一看就懂了,还是代码来的直接啊。

#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;
}

抱歉!评论已关闭.