Skip to content

Latest commit

 

History

History
88 lines (65 loc) · 1.69 KB

max_element.md

File metadata and controls

88 lines (65 loc) · 1.69 KB

#max_element

namespace std {

  template<class ForwardIterator>
  ForwardIterator max_element(ForwardIterator first, ForwardIterator last);

  template<class ForwardIterator, class Compare>
  ForwardIterator max_element(ForwardIterator first, ForwardIterator last, Compare comp);

}

##概要 [first, last)の範囲において、最大要素を指すイテレータを取得する。

##戻り値 *j < *iもしくはcomp(*j, *i)の比較によって最大と判断された要素を指すイテレータ

##計算量 max((last - first) - 1, 0)回の比較を行う

##例

#include <cassert>
#include <algorithm>
#include <vector>

int main()
{
  std::vector<int> v = {3, 1, 4};

  decltype(v)::iterator i = std::max_element(v.begin(), v.end());
  assert(*i == 4);

  decltype(v)::iterator j = std::max_element(v.begin(), v.end(), [](int a, int b) {
                              return a > b;
                            });
  assert(*j == 1);
}
  • max_element[color ff0000]

###出力

##実装例

template <class ForwardIterator>
ForwardIterator max_element(ForwardIterator first, ForwardIterator last)
{
  if (first == last)
    return first;

  ForwardIterator result = first++;
  for (; first != last; ++first) {
    if (*result < *first) {
      result = first;
    }
  }
  return result;
}

template <class ForwardIterator, class Compare>
ForwardIterator max_element(ForwardIterator first, ForwardIterator last, Compare comp)
{
  if (first == last)
    return first;

  ForwardIterator result = first++;
  for (; first != last; ++first) {
    if (comp(*result, *first)) {
      result = first;
    }
  }
  return result;
}

##参照