There is `std::minmax_element` algorithm for this function which is faster and already implemented.
```c++
std::pair const minmax = std::minmax_element(elements.cbegin(), elements.cend(),
    [&mesh](MeshLib::Element* a, MeshLib::Element* b)
    {
        return a->getValue() < b->getValue();
    });
return std::make_pair(minmax.first, minmax.second);