std::minmax_element
![]() |
Questa pagina è stata tradotta in modo automatico dalla versione in ineglese della wiki usando Google Translate.
La traduzione potrebbe contenere errori e termini strani. Muovi il puntatore sopra al testo per vedere la versione originale. Puoi aiutarci a correggere gli gli errori. Per ulteriori istruzioni clicca qui. |
Elemento definito nell'header <algorithm>
|
||
template< class ForwardIt > std::pair<ForwardIt,ForwardIt> |
(1) | |
template< class ForwardIt, class Compare > std::pair<ForwardIt,ForwardIt> |
(2) | |
[first, last)
gamma. La prima versione utilizza operator< per confrontare i valori, la seconda versione utilizza la funzione di confronto dato comp
.[first, last)
. The first version uses operator< to compare the values, the second version uses the given comparison function comp
.You can help to correct and verify the translation. Click here for instructions.
Indice |
[modifica] Parametri
first, last | - | iteratori forward che definiscono l'intervallo di esaminare
Original: forward iterators defining the range to examine The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
cmp | - | comparison function which returns true if 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); The signature does not need to have const &, but the function must not modify the objects passed to it. |
Type requirements | ||
-ForwardIt must meet the requirements of ForwardIterator .
|
[modifica] Valore di ritorno
You can help to correct and verify the translation. Click here for instructions.
[modifica] Complessità
You can help to correct and verify the translation. Click here for instructions.
[modifica] Possibile implementazione
First version |
---|
template<class ForwardIt> std::pair<ForwardIt, ForwardIt> minmax_element(ForwardIt first, ForwardIt last) { typedef typename std::iterator_traits<ForwardIt>::value_type value_t; return std::minmax_element(first, last, std::less<value_t>()); } |
Second version |
template<class ForwardIt, class Compare> std::pair<ForwardIt, ForwardIt> minmax_element(ForwardIt first, ForwardIt last, Compare comp) { std::pair<ForwardIt, ForwardIt> result(first, first); if (first == last) return result; if (++first == last) return result; if (comp(*first, *result.first)) { result.second = result.first; result.first = first; } else { result.second = first; } while (++first != last) { ForwardIt i = first; if (++first == last) { if (comp(*i, *result.first)) result.first = i; else if (!(comp(*i, *result.second))) result.second = i; break; } else { if (comp(*first, *i)) { if (comp(*first, *result.first)) result.first = first; if (!(comp(*i, *result.second))) result.second = i; } else { if (comp(*i, *result.first)) result.first = i; if (!(comp(*first, *result.second))) result.second = first; } } } return result; } |
[modifica] Esempio
#include <algorithm> #include <iostream> #include <vector> int main() { std::vector<int> v = { 3, 9, 1, 4, 2, 5, 9 }; auto result = std::minmax_element(v.begin(), v.end()); std::cout << "min element at: " << (result.first - v.begin()) << '\n'; std::cout << "max element at: " << (result.second - v.begin()) << '\n'; }
Output:
min element at: 2 max element at: 6
[modifica] Vedi anche
restituisce l'elemento più piccolo in un intervallo Original: returns the smallest element in a range The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (funzione di modello) | |
restituisce l'elemento più grande in un intervallo Original: returns the largest element in a range The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (funzione di modello) |