std::min_element(3)
π std::min_element β NAME π
okay so this page is std::min_element(3), straight out of the C++ Standard Library manual, and bestie it is literally the "find me the smallest thing in this range" function. that's the whole vibe. periodt.
β¨ Synopsis β¨
girl. look at this. four whole overloads. it's giving main character energy:
Defined in header <algorithm>
template< class ForwardIt >
ForwardIt min_element( ForwardIt first, ForwardIt last ); (1) (constexpr since C++17)
template< class ExecutionPolicy, class ForwardIt >
ForwardIt min_element( ExecutionPolicy&& policy, (2) (since C++17)
ForwardIt first, ForwardIt last );
template< class ForwardIt, class Compare >
ForwardIt min_element( ForwardIt first, ForwardIt last, (3) (constexpr since C++17)
Compare comp );
template< class ExecutionPolicy, class ForwardIt, class Compare >
ForwardIt min_element( ExecutionPolicy&& policy, (4) (since C++17)
ForwardIt first, ForwardIt last,
Compare comp );
so what does it do? it finds the smallest element in the range [first, last). that's it. that's the function. π
- 1) elements are compared using
operator<(until C++20), and usingstd::less{}(since C++20). - 3) elements are compared using the comparison function
comp. - 2,4) same as (1,3), but executed according to
policy.
and check this β these overloads (2,4) only even participate in overload resolution if:
std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> is true. (until C++20)
std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> is true. (since C++20)
iykyk π
πΈ Parameters πΈ
- first, last β forward iterators defining the range to examine. these are your "where do i start, where do i stop" besties.
- policy β the execution policy to use. see execution policy for details. π
- comp β a comparison function object (i.e. an object that satisfies the requirements of Compare) which returns
trueif the first argument is less than the second.
the signature of the comparison function should be equivalent to the following:
bool cmp(const Type1& a, const Type2& b);
okay so while the signature does not need to have const&, the function must not modify the objects passed to it, and it must be able to accept all values of type (possibly const) Type1 and Type2 regardless of value category (thus, Type1& is not allowed, nor is Type1 unless for Type1 a move is equivalent to a copy (since C++11)). she's picky, she's iconic π
also the types Type1 and Type2 must be such that an object of type ForwardIt can be dereferenced and then implicitly converted to both of them. no cap, that part matters.
π¦ Type requirements π¦
ForwardItmust meet the requirements of LegacyForwardIterator.
π Return value π
iterator to the smallest element in the range [first, last). simple. π₯
but here's the fine print: if several elements in the range are equivalent to the smallest element, it returns the iterator to the first such element. and it returns last if the range is empty. okay so it never just ghosts you with nothing β you always get an iterator back π«Ά
β Complexity β
given N as std::distance(first, last):
- 1,2) exactly
max(N-1,0)comparisons usingoperator<(until C++20),std::less{}(since C++20). - 3,4) exactly
max(N-1,0)applications of the comparison functioncomp.
so it's linear, queen. it's not out here doing anything extra. max(N-1,0) and that's final. π
π Exceptions π
the overloads with a template parameter named ExecutionPolicy report errors as follows:
- if execution of a function invoked as part of the algorithm throws an exception and
ExecutionPolicyis one of the standard policies,std::terminateis called. for any otherExecutionPolicy, the behavior is implementation-defined. - if the algorithm fails to allocate memory,
std::bad_allocis thrown.
okay real talk on that first bullet: std::terminate is not a vibe check, it's the end of your whole program. so don't be throwing out of these unless you know exactly what you're doing π
π§ Possible implementation π§
here's min_element (1), the no-frills one:
template<class ForwardIt>
ForwardIt min_element(ForwardIt first, ForwardIt last)
{
if (first == last)
return last;
ForwardIt smallest = first;
while (++first != last)
if (*first < *smallest)
smallest = first;
return smallest;
}
and here's min_element (3), the one that lets you bring your own comparator:
template<class ForwardIt, class Compare>
ForwardIt min_element(ForwardIt first, ForwardIt last, Compare comp)
{
if (first == last)
return last;
ForwardIt smallest = first;
while (++first != last)
if (comp(*first, *smallest))
smallest = first;
return smallest;
}
look at her go. literally so clean. π
π Example π
// Run this code
#include <algorithm>
#include <iostream>
#include <vector>
int main()
{
std::vector<int> v{3, 1, -4, 1, 5, 9};
std::vector<int>::iterator result = std::min_element(v.begin(), v.end());
std::cout << "min element has value " << *result << " and index ["
<< std::distance(v.begin(), result) << "]\n";
}
Output:
min element has value -4 and index [2]
ate. no notes. π β¨
π Defect reports π
the following behavior-changing defect reports were applied retroactively to previously published C++ standards. history lesson time, bestie:
- LWG 212 β applied to
C++98β behavior as published: the return value was not specified if[first, last)is empty β correct behavior: returnslastin this case. - LWG 2150 β applied to
C++98β behavior as published: the iterator to the first non-greatest element was returned β correct behavior: corrected the return value.
the way they had to go back and fix that π iconic of them to document it though.
πΈ See also πΈ
max_elementβ returns the largest element in a range (function template)minmax_elementβ returns the smallest and the largest elements in a range (C++11) (function template)minβ returns the smaller of the given values (function template)ranges::min_elementβ returns the smallest element in a range (C++20) (niebloid)
okay that's the whole page, you made it to the end, i'm so proud of you π«Άπ this was std::min_element(3) from the C++ Standard Library manual β sourced from http://cppreference.com, dated 2024.06.10, filed under std::min_element(3). go find your smallest element, bestie. slay ππ
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms