Skip to content
Snippets Groups Projects
Commit fd080e1f authored by Ansgar Burchardt's avatar Ansgar Burchardt
Browse files

Use `std::max_element` and `std::min_element`

This avoids using the uninitialized variable `m` in the loop. Note that
calling `max_value` or `min_value` on an empty list will now result in
undefined behavior as the end iterator is then dereferenced.
parent b623327e
Branches
Tags
2 merge requests!212Fix link to build system doc,!183Fix `max_value` and `min_value`
Pipeline #
......@@ -4,6 +4,7 @@
#define DUNE_COMMON_RANGE_UTILITIES_HH
#include <dune/common/typetraits.hh>
#include <algorithm>
#include <utility>
#include <type_traits>
#include <bitset>
......@@ -30,11 +31,8 @@ namespace Dune
typename std::enable_if<is_range<T>::value, int>::type = 0>
typename T::value_type
max_value(const T & v) {
using std::max;
typename T::value_type m;
for (const auto & e : v)
m = max(e,m);
return m;
using std::max_element;
return *max_element(v.begin(), v.end());
}
template <typename T,
......@@ -50,11 +48,8 @@ namespace Dune
typename std::enable_if<is_range<T>::value, int>::type = 0>
typename T::value_type
min_value(const T & v) {
using std::min;
typename T::value_type m;
for (const auto & e : v)
m = min(e,m);
return m;
using std::min_element;
return *min_element(v.begin(), v.end());
}
template <typename T,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment