请问STL 中Algorithms里max_element 怎么用?

来源:百度知道 编辑:UC知道 时间:2024/06/01 05:43:51
#include <algorithm>
iterator max_element( iterator start, iterator end );
iterator max_element( iterator start, iterator end, BinPred p );
我是菜鸟!
想问问里面的第三个参数BinPred p 是什么意思啊?

我想查找一个结构体list中一个INT域的最大元素怎么搞啊?

struct ball
{
char ballcolo;
int numsegment;
};

list <ball> a;

想找a中numsegment域的最大值,用max_element,可以吗?怎么写啊?

max_element默认用less排序
  所以max_element(ite1,ite2)相当于max_element(ite1,ite2,less <T> ());
  如果你无聊,就把max_element()最后参数设置成greater试试
  比如1,2,3,4用:
  <dl class=code><dt>C/C++ code</dt><dd><pre>
  <!--

  Code highlighting produced by Actipro CodeHighlighter (freeware)
  www CodeHighlighter.com/

  -->max_element(ite1, ite2, less<int>());
  </pre></dd></dl>和
  <dl class=code><dt>C/C++ code</dt><dd><pre>
  <!--

  Code highlighting produced by Actipro CodeHighlighter (freeware)
  www CodeHighlighter.com/

  -->max_element(ite1, ite2);
  </pre></dd></dl>
  得到一样的结果,都是最大值,4
  如果用
  <dl class=code><dt>C/C++ code</dt><dd><pre>
  <!--

  Code highlighting produced by Actipro CodeHighlighter (freeware)
  www CodeHighlight