Min c какая библиотека
std::min is defined in the header file <algorithm> and is used to find out the smallest of the number passed to it. It returns the first of them, if there are more than one.
It can be used in following 3 manners:
- It compares the two numbers passed in its arguments and returns the smaller of the two, and if both are equal, then it returns the first one.
- It can also compare the two numbers using a binary function , which is defined by the user, and then passed as argument in std::min().
- It is also useful if we want to find the smallest element in a given list, and it returns the first one if there are more than one present in the list.
The three versions are as defined below:
- For comparing elements using < :
Syntax:
Time complexity :- O(1)
Auxiliary Space :- O(1)
2. For comparing elements using a pre-defined function:
Syntax:
Time complexity :- O(1)
Auxiliary Space:- O(1)
3. For finding the minimum element in a list:
Syntax:
Time complexity :- O(n)
Auxiliary Space :- O(1)
std::min
(1,3) версии используют operator< для сравнения значений, (2,4) версии используют данную функцию сравнения comp .
Parameters
| a, b | — | сравниваемые значения |
| ilist | — | список инициализаторов со значениями для сравнения |
| cmp | — | функция сравнения объект (т.е. объект , который удовлетворяет требованиям сравнения ) , который возвращает true если является a less than b . |
Подпись функции сравнения должна быть эквивалентна следующей:
bool cmp(const Type1 &a, const Type2 &b);
Хотя подпись не должна иметь const & , функция не должна изменять передаваемые ей объекты и должна иметь возможность принимать все значения типа (возможно, const) Type1 и Type2 независимо от категории значения (таким образом, Type1 & не допускается Кроме того, Type1 не является исключением, если для Type1 перемещение эквивалентно копии (начиная с C ++ 11)).
Типы Type1 и Type2 должны быть такими, чтобы объект типа T мог быть неявно преобразован в них обоих.
Use of min and max functions in C++
From C++, are std::min and std::max preferable over fmin and fmax ? For comparing two integers, do they provide basically the same functionality?
Do you tend to use one of these sets of functions or do you prefer to write your own (perhaps to improve efficiency, portability, flexibility, etc.)?
The C++ Standard Template Library (STL) declares the min and max functions in the standard C++ algorithm header.
The C standard (C99) provides the fmin and fmax function in the standard C math.h header.
14 Answers 14
fmin and fmax are specifically for use with floating point numbers (hence the «f»). If you use it for ints, you may suffer performance or precision losses due to conversion, function call overhead, etc. depending on your compiler/platform.
std::min and std::max are template functions (defined in header <algorithm> ) which work on any type with a less-than ( < ) operator, so they can operate on any data type that allows such a comparison. You can also provide your own comparison function if you don’t want it to work off < .
This is safer since you have to explicitly convert arguments to match when they have different types. The compiler won’t let you accidentally convert a 64-bit int into a 64-bit float, for example. This reason alone should make the templates your default choice. (Credit to Matthieu M & bk1e)
Even when used with floats the template may win in performance. A compiler always has the option of inlining calls to template functions since the source code is part of the compilation unit. Sometimes it’s impossible to inline a call to a library function, on the other hand (shared libraries, absence of link-time optimization, etc.).
std:: min
Overloads (1,3) use operator< to compare the values, overloads (2,4) use the given comparison function comp .
Contents
[edit] Parameters
| a, b | — | the values to compare |
| ilist | — | initializer list with the values to compare |
| cmp | — | comparison function object (i.e. an object that satisfies the requirements of Compare ) which returns true if a is less than b . |
The signature of the comparison function should be equivalent to the following:
bool cmp ( const Type1 & a, const Type2 & b ) ;
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) ).
The types Type1 and Type2 must be such that an object of type T can be implicitly converted to both of them.