cpp/algorithm/max element — различия между версиями
Материал из cppreference.com
P12bot (обсуждение | вклад) м (Improve translation of headings) |
Dima (обсуждение | вклад) |
||
(не показаны 4 промежуточные версии 4 участников) | |||
Строка 1: | Строка 1: | ||
− | |||
{{cpp/title|max_element}} | {{cpp/title|max_element}} | ||
{{cpp/algorithm/navbar}} | {{cpp/algorithm/navbar}} | ||
− | {{ | + | {{begin}} |
− | {{ | + | {{header | algorithm}} |
− | {{ | + | {{| num=1 |
− | template< class ForwardIt > | + | | |
− | ForwardIt max_element( ForwardIt first, ForwardIt last ); | + | template< class ForwardIt > |
+ | ForwardIt max_element(ForwardIt first, ForwardIt last); | ||
}} | }} | ||
− | {{ | + | {{ |
+ | |||
+ | |||
+ | |||
+ | |||
+ | | num= 2 | ||
+ | |||
+ | |||
+ | |||
+ | | | ||
template< class ForwardIt, class Compare > | template< class ForwardIt, class Compare > | ||
− | ForwardIt max_element( ForwardIt first, ForwardIt last, Compare | + | ForwardIt max_element(ForwardIt first, ForwardIt last, Compare ); |
}} | }} | ||
− | {{ | + | {{ |
+ | |||
+ | |||
+ | |||
+ | end}} | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
− | + | {{tt|}}. | |
+ | {{|}} | ||
+ | ,{{tt|}} . | ||
===Параметры=== | ===Параметры=== | ||
− | {{ | + | {{begin}} |
− | {{ | + | {{| first, last | итераторыдля }} |
− | {{ | + | }} |
− | {{ | + | {{cmp | cmp | p1=ForwardIt }} |
− | {{ | + | {{hreq}} |
− | {{ | + | {{req concept | ForwardIt | ForwardIterator}} |
+ | {{end}} | ||
===Возвращаемое значение=== | ===Возвращаемое значение=== | ||
− | + | {{tt|[first, last)}}. , . {{tt|last}}. | |
===Сложность=== | ===Сложность=== | ||
− | + | {{math|max(N-1,0)}} сравнений, где {{c|N {{=}} std::distance(first, last)}}. | |
+ | |||
+ | |||
+ | {{|=}} | ||
===Возможная реализация=== | ===Возможная реализация=== | ||
Строка 50: | Строка 75: | ||
| 2= | | 2= | ||
template<class ForwardIt, class Compare> | template<class ForwardIt, class Compare> | ||
− | ForwardIt max_element(ForwardIt first, ForwardIt last, | + | ForwardIt max_element(ForwardIt first, ForwardIt last, |
− | + | Compare ) | |
{ | { | ||
if (first == last) { | if (first == last) { | ||
Строка 59: | Строка 84: | ||
++first; | ++first; | ||
for (; first != last; ++first) { | for (; first != last; ++first) { | ||
− | if ( | + | if ((*largest, *first)) { |
largest = first; | largest = first; | ||
} | } | ||
Строка 83: | Строка 108: | ||
int main() | int main() | ||
{ | { | ||
− | std::vector<int> v{ 3, 1, -14, 1, 5, 9 }; | + | std::vector<int> v{ 3, 1, -14, 1, 5, 9 }; |
std::vector<int>::iterator result; | std::vector<int>::iterator result; | ||
result = std::max_element(v.begin(), v.end()); | result = std::max_element(v.begin(), v.end()); | ||
− | std::cout << " | + | std::cout << ": " << std::distance(v.begin(), result) << '\n'; |
result = std::max_element(v.begin(), v.end(), abs_compare); | result = std::max_element(v.begin(), v.end(), abs_compare); | ||
− | std::cout << " | + | std::cout << "() : " << std::distance(v.begin(), result); |
} | } | ||
| output= | | output= | ||
− | + | : 5 | |
− | + | () : 2 | |
}} | }} | ||
===См. также=== | ===См. также=== | ||
− | {{ | + | {{begin}} |
− | {{ | + | {{| cpp/algorithm/min_element}} |
− | {{ | + | {{| cpp/algorithm/minmax_element}} |
− | {{ | + | {{| cpp/algorithm/max}} |
− | {{ | + | |
+ | {{end}} | ||
− | |||
[[en:cpp/algorithm/max element]] | [[en:cpp/algorithm/max element]] | ||
+ | |||
[[es:cpp/algorithm/max element]] | [[es:cpp/algorithm/max element]] | ||
[[fr:cpp/algorithm/max element]] | [[fr:cpp/algorithm/max element]] |