[C++] max_element()&min_element()

#include <algorithm>    // std::min_element, std::max_element
bool myfn(int a,int b){
  return a < b;
}
int myints[] = {3,7,2,5,6,4,9};

// using default comparison:
cout << "The smallest " << *min_element(myints,myints+7) << endl;
cout << "The largest "  << *max_element(myints,myints+7) << endl;

// using function myfn as comp:
cout << "The smallest " << *min_element(myints,myints+7,myfn) << endl;
cout << "The largest "  << *max_element(myints,myints+7,myfn) << endl;

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *