STL algorithm minmax, minmax_element (36), stlalgorithm

Source: Internet
Author: User

STL algorithm minmax, minmax_element (36), stlalgorithm

Minmax prototype:

Std: minmax
  • C ++ 11
  • C ++ 14
Default (1)
template <class T>  pair <const T&,const T&> minmax (const T& a, const T& b);
Custom (2)
template <class T, class Compare>  pair <const T&,const T&> minmax (const T& a, const T& b, Compare comp);
Initializer list (3)
template <class T>  pair<T,T> minmax (initializer_list<T> il);template <class T, class Compare>  pair<T,T> minmax (initializer_list<T> il, Compare comp);
This function returns a pair. The value of the first element of the pair is the minimum value in a and B, and the value of second is the maximum value in a. B.

Use operator <for comparison.

If a = B, make_pair (a, B) is returned ).

For (3), return the pair of the minimum and maximum values in the initialization list. If the maximum value is more than one, firstf returns the minimum value of the first occurrence, and second returns the maximum value of the last occurrence.

The behavior is similar:

template <class T> pair <const T&,const T&> minmax (const T& a, const T& b) {  return (b<a) ? std::make_pair(b,a) : std::make_pair(a,b);}
A simple example:

#include <iostream>#include <algorithm>#include <vector>using namespace std;void minmax2(){    auto it=minmax(20,10);   cout<<" auto it=minmax(20,10)"<<endl;   cout<<"it.first()="<<it.first<<" ,it.second="<<it.second<<endl;   auto it2=minmax({2,3,5,7,1,3,1});   cout<<"auto it2=minmax({2,3,5,7,1,3,1})"<<endl;   cout<<"it2.first()="<<it2.first<<" ,it2.second="<<it2.second<<endl;}
Run:





Minmax_element prototype:

Std: minmax_element
Default (1)
template <class ForwardIterator>  pair<ForwardIterator,ForwardIterator>    minmax_element (ForwardIterator first, ForwardIterator last);
Custom (2)
template <class ForwardIterator, class Compare>  pair<ForwardIterator,ForwardIterator>    minmax_element (ForwardIterator first, ForwardIterator last, Compare comp);
This function is a pair composed of elements that return the maximum and minimum values in a specified range. If the maximum value is more than one, firstf returns the first iterator with the minimum value, second returns the last iterator with the maximum value.

Use operator <for comparison.

A simple example:
#include <iostream>#include <algorithm>#include <vector>using namespace std;void minmaxelement(){    vector<int> vi{3,5,4,1,3,1,9,9,5};    cout<<"vi=";    for(int i:vi)        cout<<i<<" ";    cout<<endl;    auto it=minmax_element(vi.begin(),vi.end());   cout<<" auto it=minmax_element(vi.begin(),vi.end())"<<endl;   cout<<"*it.first="<<*it.first<<" ,*it.second="<<*it.second<<endl;    cout<<"*(it.first-1)="<<*(it.first-1)<<" ,*(it.second-1)="<<*(it.second-1)<<endl;}
Run: we can see that firstf returns the first iterator with the minimum value and second returns the last iterator with the maximum value.

------------------------------------------------------------------

// For more instructions on writing errors or poor information, you can leave a message below or click the email address in the upper left corner to send an email to me, pointing out my errors and deficiencies, so that I can modify them, thank you for sharing it.

Reprinted please indicate the source: http://blog.csdn.net/qq844352155

Author: unparalleled

Email: coderguang@gmail.com

Yu GDUT

------------------------------------------------------------------







1. Write a program and use MINMAX to solve the minimum and maximum values of the array, and test the array with the actual array. 2. In the algorithm

# Include <stdio. h>
Void minmax (int array [], int begin, int end, int & min, int & max, int & count)
{
Int mid = (begin + end)/2;
If (begin = end)
{
Count ++;
If (array [mid] <min ){
Min = array [mid];
} Else {
If (array [mid]> max)
Max = array [mid];
Count ++;
}
Return;
}
Minmax (array, begin, mid, min, max, count );
Minmax (array, mid + 1, end, min, max, count );
}

Void main ()
{
Int array [10] = {9, 8, 7, 6, 5, 4, 3, 2 };
Int min = array [0], max =-1, count = 0;
Minmax (array, 0, 9, min, max, count );
Printf ("min = % d, max = % d, count = % d \ n", min, max, count );
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.