Пространства имён
Варианты
Действия

cpp/algorithm/max element — различия между версиями

Материал из cppreference.com
< cpp‎ | algorithm
м (Improve translation of headings)
 
(не показаны 4 промежуточные версии 4 участников)
Строка 1: Строка 1:
{{tr_note}}
 
 
{{cpp/title|max_element}}
 
{{cpp/title|max_element}}
 
{{cpp/algorithm/navbar}}
 
{{cpp/algorithm/navbar}}
{{ddcl list begin}}
+
{{begin}}
{{ddcl list header | algorithm}}
+
{{header | algorithm}}
{{ddcl list item | num=1 |
+
{{| num=1
template< class ForwardIt >
+
|
ForwardIt max_element( ForwardIt first, ForwardIt last );
+
template< class ForwardIt >  
 +
ForwardIt max_element(ForwardIt first, ForwardIt last);
 
}}
 
}}
{{ddcl list item | num=2 |
+
{{
 +
 +
 +
 +
 +
| num= 2
 +
 +
 +
 +
|
 
template< class ForwardIt, class Compare >
 
template< class ForwardIt, class Compare >
ForwardIt max_element( ForwardIt first, ForwardIt last, Compare comp );
+
ForwardIt max_element(ForwardIt first, ForwardIt last, Compare );
 
}}
 
}}
{{ddcl list end}}
+
{{
 +
 +
 +
 +
end}}
 +
 +
 +
 +
 +
 
 +
  
Finds the greatest element in the range {{tt|[first, last)}}. The first version uses {{c|operator<}} to compare the values, the second version uses the given comparison function {{tt|comp}}.
+
{{tt|}}.
 +
{{|}}
 +
,{{tt|}} .
  
 
===Параметры===
 
===Параметры===
{{param list begin}}
+
{{begin}}
{{param list item | first, last |{{tr| вперед итераторы определении диапазона для изучения| forward iterators defining the range to examine}}}}
+
{{| first, last | итераторыдля }}
{{param list cmp | cmp | p1=ForwardIt | if {{tt|*a}} is ''less'' than {{tt|*b}}}}
+
}}
{{param list hreq}}
+
{{cmp | cmp | p1=ForwardIt }}  
{{param list req concept | ForwardIt | ForwardIterator}}
+
{{hreq}}
{{param list end}}
+
{{req concept | ForwardIt | ForwardIterator}}
 +
{{end}}
  
 
===Возвращаемое значение===
 
===Возвращаемое значение===
Iterator to the greatest element in the range {{tt|[first, last)}}. If several elements in the range are equivalent to the greatest element, returns the iterator to the first such element. Returns {{tt|last}} if the range is empty.
+
{{tt|[first, last)}}. , . {{tt|last}}.  
  
 
===Сложность===
 
===Сложность===
{{tr|Именно {{math|max(N-1,0)}} сравнений, где {{c|N {{=}} std::distance(first, last)}}.|Exactly {{math|max(N-1,0)}} comparisons, where {{c|N {{=}} std::distance(first, 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 comp)
+
Compare )
 
{
 
{
 
     if (first == last) {
 
     if (first == last) {
Строка 59: Строка 84:
 
     ++first;
 
     ++first;
 
     for (; first != last; ++first) {
 
     for (; first != last; ++first) {
         if (comp(*largest, *first)) {
+
         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 << "max element at: " << std::distance(v.begin(), result) << '\n';
+
     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 << "max element (absolute) at: " << std::distance(v.begin(), result);
+
     std::cout << "() : " << std::distance(v.begin(), result);
 
}
 
}
 
  | output=
 
  | output=
max element at: 5
+
: 5
max element (absolute) at: 2
+
() : 2
 
}}
 
}}
  
 
===См. также===
 
===См. также===
{{dcl list begin}}
+
{{begin}}
{{dcl list template | cpp/algorithm/dcl list min_element}}
+
{{| cpp/algorithm/min_element}}
{{dcl list template | cpp/algorithm/dcl list minmax_element}}
+
{{| cpp/algorithm/minmax_element}}
{{dcl list template | cpp/algorithm/dcl list max}}
+
{{| cpp/algorithm/max}}
{{dcl list end}}
+
 
 +
{{end}}
  
[[de:cpp/algorithm/max element]]
 
 
[[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]]

Текущая версия на 06:48, 29 января 2018