std::min(3)
std::min π (the C++ Standard Library page, bestie)
okay so first of all, this is std::min from the C++ Standard Library, and she is literally the most iconic little function in <algorithm>. like yes she picks the smaller one. that's her job. that's her whole personality and she ATE. β¨
β¨ Synopsis β¨
she lives here, no cap:
Defined in header <algorithm>
template< class T > (1) (constexpr since C++14)
const T& min( const T& a, const T& b );
template< class T, class Compare > (2) (constexpr since C++14)
const T& min( const T& a, const T& b, Compare comp );
template< class T > (3) (since C++11)
T min( std::initializer_list<T> ilist ); (constexpr since C++14)
template< class T, class Compare > (4) (since C++11)
T min( std::initializer_list<T> ilist, Compare comp ); (constexpr since C++14)
so what does she do? returns the smaller of the given values. that's it. that's the tweet. π«Ά
1,2)β returns the smaller ofaandb.1)β usesoperator<to compare the values. ifTis not LessThanComparable, the behavior is undefined π (like, go touch grass, undefined behavior is not the vibe).2)β uses the comparison functioncompto compare the values.
3,4)β returns the smallest of the values in initializer listilist. ifilist.size()is zero, orTis not CopyConstructible, the behavior is undefined π.3)β usesoperator<to compare the values. ifTis not LessThanComparable, the behavior is undefined.4)β uses the comparison functioncompto compare the values.
πΈ Parameters πΈ
a,bβ the values to compare π―ββοΈilistβ initializer list with the values to comparecompβ comparison function object (i.e. an object that satisfies the requirements of Compare) which returnstrueifais less thanb.
the signature of the comparison function should be equivalent to the following:
bool cmp(const Type1& a, const Type2& b);
okay so listen, this part is kind of a lot but it matters: while the signature does not need to have const&, the function must not modify the objects passed to it, and 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)). π§
and the types Type1 and Type2 must be such that an object of type T can be implicitly converted to both of them. iykyk. β¨
π Return value π
1,2)β the smaller ofaandb. if the values are equivalent, returnsa. (she's loyal to the left, periodt.)3,4)β the smallest value inilist. if several values are equivalent to the smallest, returns the leftmost such value. queen of consistency π
π¦ Complexity π¦
1)β exactly one comparison usingoperator<.2)β exactly one application of the comparison functioncomp.3,4)β given (\scriptsize N)N asilist.size():3)β exactly (\scriptsize N-1)N-1 comparisons usingoperator<.4)β exactly (\scriptsize N-1)N-1 applications of the comparison functioncomp.
not her doing extra work for no reason, she's efficient π
π Possible implementation π
here's how she'd look if you wrote her yourself, lowkey:
min (1)
template<class T>
const T& min(const T& a, const T& b)
{
return (b < a) ? b : a;
}
min (2)
template<class T, class Compare>
const T& min(const T& a, const T& b, Compare comp)
{
return (comp(b, a)) ? b : a;
}
min (3)
template<class T>
T min(std::initializer_list<T> ilist)
{
return *std::min_element(ilist.begin(), ilist.end());
}
min (4)
template<class T, class Compare>
T min(std::initializer_list<T> ilist, Compare comp)
{
return *std::min_element(ilist.begin(), ilist.end(), comp);
}
β οΈ Notes β οΈ
bestie. listen to me. capturing the result of std::min by reference produces a dangling reference if one of the parameters is a temporary and that parameter is returned. π¨ this is the part where you don't get to be cute.
int n = -1;
const int& r = std::min(n + 2, n * 2); // r is dangling
like... r is hanging on by a thread and there is no thread. π
π Example π
// Run this code
#include <algorithm>
#include <iostream>
#include <string_view>
int main()
{
std::cout << "smaller of 10 and 010 is " << std::min(10, 010) << '\n'
<< "smaller of 'd' and 'b' is '" << std::min('d', 'b') << "'\n"
<< "shortest of \"foo\", \"bar\", and \"hello\" is \""
<< std::min({"foo", "bar", "hello"},
[](const std::string_view s1, const std::string_view s2)
{
return s1.size() < s2.size();
}) << "\"\n";
}
Output:
smaller of 10 and 010 is 8
smaller of 'd' and 'b' is 'b'
shortest of "foo", "bar", and "hello" is "foo"
(and yes, 010 is octal, that's why it's 8, don't come for me, that's just the example doing its thing π)
π«Ά Defect reports π«Ά
the following behavior-changing defect reports were applied retroactively to previously published C++ standards. history said what it said and then got corrected, we love growth π
LWG 281β applied to C++98. behavior as published:Twas required to be CopyConstructible for overloads(1,2). correct behavior: not required. slay for fixing that β¨
π See also π
maxβ returns the greater of the given values (function template)minmaxβ returns the smaller and larger of two elements (C++11) (function template)min_elementβ returns the smallest element in a range (function template)clampβ clamps a value between a pair of boundary values (C++17) (function template)ranges::minβ returns the smaller of the given values (C++20) (niebloid)
that's the whole page bestie, go pick the smaller one with confidence π
source + version + date sign-off: straight from http://cppreference.com, version 2024.06.10, and this page is dated 2024.06.10 β std::min(3), mwah. πβ
π¦ 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