If your data are stored in a vector, then the use of a reverse iterator should suffice, as already suggested.
More generally, the Standard Library does not provide a min_element_last function, as also commented in 1. In this respect, a possible implementation of min_element_last may read:
template <typename I> using ValueType = typename std::iterator_traits<I>::value_type; template <typename I, typename R> // I models ForwardIterator // R models StrictWeakOrdering on ValueType<I> I min_element_last(I first, I last, R cmp) { if (first == last) return last; I curr = first; ++first; while (first != last) { if (!cmp(*curr, *first)) { curr = first; } ++first; } return curr; } template <typename I> // I models ForwardIterator // ValueType<I> models TotallyOrdered I min_element_last(I first, I last) { using T = ValueType<I>; return min_element_last(first, last, std::less<T>()); }
The advantage would be the possibility of using min_element_last also with iterators that only model the ForwardIterator concept.
min_elementover reverse iterators; you'll get last occurrence for free.compdoes not satisfy the requirements of a strict weak ordering. Your program therefore exhibits undefined behavior.if (a <= b)--std::min_elementwants to know ifa < b, not ifa <= b. Your code answers the wrong question.