STL algorithm演算法max,max_elements(33),stlalgorithm

來源:互聯網
上載者:User

STL algorithm演算法max,max_elements(33),stlalgorithm

max原型:

std::max
  • C++98
  • C++11
  • C++14
default (1)
template <class T> const T& max (const T& a, const T& b);
custom (2)
template <class T, class Compare>  const T& max (const T& a, const T& b, Compare comp);
initializer list (3)
template <class T> T max (initializer_list<T> il);template <class T, class Compare>  T max (initializer_list<T> il, Compare comp);
該函數返回範圍或者兩個數中最大的一個。

對於(1),如果兩個數相等,則返回a;

其行為類似於:

template <class T> const T& max (const T& a, const T& b) {  return (a<b)?b:a;     // or: return comp(a,b)?b:a; for version (2)}
一個簡單的例子:

#include <iostream>#include <algorithm>#include <vector>using namespace std;void max2(){   cout<<"max(10,22)="<<max(10,22)<<endl;   cout<<"max({1,2,5,7,9,999,888})="<<max({1,2,5,7,9,999,888})<<endl;}
運行:




max_elements原型:

std::max_element
default (1)
template <class ForwardIterator>  ForwardIterator max_element (ForwardIterator first, ForwardIterator last);
custom (2)
template <class ForwardIterator, class Compare>  ForwardIterator max_element (ForwardIterator first, ForwardIterator last,                               Compare comp);
返回範圍內值最大那個元素的迭代器,如果存在多個相同最大值,則返回第一個。

(max返回的是元素,這個返回的是迭代器)

如果範圍為空白,則返回last.

使用operator<進行比較。

其行為類似於:

template <class ForwardIterator>  ForwardIterator max_element ( ForwardIterator first, ForwardIterator last ){  if (first==last) return last;  ForwardIterator largest = first;  while (++first!=last)    if (*largest<*first)    // or: if (comp(*largest,*first)) for version (2)      largest=first;  return largest;}
一個簡單的例子:

#include <iostream>#include <algorithm>#include <vector>using namespace std;void maxelement(){    vector<int> vi{1,1,2,3,4};    cout<<"at first vi=";    for(int i:vi)        cout<<i<<" ";    cout<<endl;    cout<<"max_element(vi.begin(),vi.end())="<<*max_element(vi.begin(),vi.end())<<endl;    cout<<"max_element(vi.begin(),vi.begin()+1)="<<*max_element(vi.begin(),vi.begin()+1)<<endl;    if(max_element(vi.end(),vi.end())==vi.end())        cout<<"max_element(vi.end(),vi.end())=vi.end()"<<endl;}
運行結果:




——————————————————————————————————————————————————————————————————

//寫的錯誤或者不好的地方請多多指導,可以在下面留言或者點擊左上方郵件地址給我發郵件,指出我的錯誤以及不足,以便我修改,更好的分享給大家,謝謝。

轉載請註明出處:http://blog.csdn.net/qq844352155

author:天下無雙

Email:coderguang@gmail.com

2014-9-17

於GDUT

——————————————————————————————————————————————————————————————————









對於STL algorithm的for_each()第三個參數問題

......,print<int>);
 
問STL 中Algorithms裡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 CodeHighlighter.com/

  -->max_element(ite1, ite2, greater<int>());
  </pre></dd></dl>
  得到的是1,哈哈,enjoy!

  啊?還是沒說如果對於結構體中的某一個域怎麼辦啊?

  不過還是謝謝你啊!呵呵
  <dl class=code><dt>C/C++ code</dt><dd><pre>

  更詳細的解決辦法參考 :
  passport.baidu.com/...094973...餘下全文>>
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.