tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
max_element.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2// SPDX-FileCopyrightText: Copyright (C) 2020 Tobias Hienzsch
3
4#ifndef TETL_ALGORITHM_MAX_ELEMENT_HPP
5#define TETL_ALGORITHM_MAX_ELEMENT_HPP
6
7#include <etl/_functional/less.hpp>
8
9namespace etl {
10
11/// \brief Finds the greatest element in the range `[first, last)`. Elements are
12/// compared using the given binary comparison function comp.
13/// \ingroup algorithm
14template <typename ForwardIt, typename Compare>
15[[nodiscard]] constexpr auto max_element(ForwardIt first, ForwardIt last, Compare comp) -> ForwardIt
16{
17 if (first == last) {
18 return last;
19 }
20
21 ForwardIt largest = first;
22 ++first;
23 for (; first != last; ++first) {
24 if (comp(*largest, *first)) {
25 largest = first;
26 }
27 }
28 return largest;
29}
30
31/// \brief Finds the greatest element in the range `[first, last)`. Elements are
32/// compared using operator<.
33/// \ingroup algorithm
34template <typename ForwardIt>
35[[nodiscard]] constexpr auto max_element(ForwardIt first, ForwardIt last) noexcept -> ForwardIt
36{
37 return etl::max_element(first, last, etl::less());
38}
39
40} // namespace etl
41
42#endif // TETL_ALGORITHM_MAX_ELEMENT_HPP
constexpr auto max_element(ForwardIt first, ForwardIt last) noexcept -> ForwardIt
Finds the greatest element in the range [first, last). Elements are compared using operator<.
Definition max_element.hpp:35
constexpr auto max_element(ForwardIt first, ForwardIt last, Compare comp) -> ForwardIt
Finds the greatest element in the range [first, last). Elements are compared using the given binary c...
Definition max_element.hpp:15
Definition adjacent_find.hpp:9
Function object for performing comparisons. Unless specialised, invokes operator< on type T....
Definition less.hpp:15