STL algorithm (algorithms): Extreme Value

Source: Internet
Author: User
STL algorithm (algorithms): Extreme Value 1, Min: returns the minimum values of the two parameters.
Prototype: Template <class T> const T & min (const T & A, const T & B );
Template <class T, class compare>
Const T & min (const T & A, const T & B, compare comp );
Example:
// Min example
# Include <iostream>
# Include <algorithm>
Using namespace STD;

Int main (){

Cout <"min (1, 2) =" <min (1, 2) <Endl;
Cout <"min (2, 1) =" <min (2, 1) <Endl;
Cout <"min ('A', 'z') =" <min ('A', 'z') <Endl;
Cout <"min (3.14, 2.72) =" <min (3.14, 2.72) <Endl;
Return 0;
}
2. MAX: returns the values of the two parameters.
Prototype:
Template <class T> const T & MAX (const T & A, const T & B );
Template <class T, class compare>
Const T & MAX (const T & A, const T & B, compare comp );
Example:
// Max example
# Include <iostream>
# Include <algorithm>
Using namespace STD;

Int main () {cout <"max (1, 2) =" <max (1, 2) <Endl;
Cout <"max (2, 1) =" <max (2, 1) <Endl;
Cout <"max ('A', 'z') =" <max ('A', 'z') <Endl;
Cout <"max (3.14, 2.73) =" <max (3.14, 2.73) <Endl;
Return 0;
}
3. min_element: returns the smallest element within the specified range (iterator ).
Prototype:
// Min_element/max_element
# Include <iostream>
# Include <algorithm>
Using namespace STD;

Bool myfn (int I, Int J) {return I <j ;}

Struct myclass {
Bool operator () (int I, Int J) {return I <j ;}
} Myobj;

Int main (){
Int myints [] = {3, 7, 2, 5, 6, 4, 9 };

// Using default comparison:
Cout <"the smallest element is" <* min_element (myints, myints + 7) <Endl;
Cout <"the largest element is" <* max_element (myints, myints + 7) <Endl;

// Using function myfn as COMP:
Cout <"the smallest element is" <* min_element (myints, myints + 7, myfn) <Endl;
Cout <"the largest element is" <* max_element (myints, myints + 7, myfn) <Endl;

// Using object myobj as COMP:
Cout <"the smallest element is" <* min_element (myints, myints + 7, myobj) <Endl; cout <"the largest element is" <* max_element (myints, myints + 7, myobj) <Endl;

Return 0;
}
4. max_element: returns the smallest element within the specified range (iterator ).
Prototype:
Template <class forwarditerator>
Forwarditerator max_element (forwarditerator first, forwarditerator last );

Template <class forwarditerator, class compare>
Forwarditerator max_element (forwarditerator first, forwarditerator last,
Compare comp );
Example:
// Min_element/max_element
# Include <iostream>
# Include <algorithm>
Using namespace STD;

Bool myfn (int I, Int J) {return I <j ;}

Struct myclass {
Bool operator () (int I, Int J) {return I <j ;}
} Myobj;

Int main (){
Int myints [] = {3, 7, 2, 5, 6, 4, 9 };

// Using default comparison:
Cout <"the smallest element is" <* min_element (myints, myints + 7) <Endl;
Cout <"the largest element is" <* max_element (myints, myints + 7) <Endl;

// Using function myfn as COMP:
Cout <"the smallest element is" <* min_element (myints, myints + 7, myfn) <Endl;
Cout <"the largest element is" <* max_element (myints, myints + 7, myfn) <Endl;

// Using object myobj as COMP:
Cout <"the smallest element is" <* min_element (myints, myints + 7, myobj) <Endl; cout <"the largest element is" <* max_element (myints, myints + 7, myobj) <Endl;

Return 0;
}
5. next_permutation: returns
Prototype:
Template <class bidirectionaliterator>
Bool next_permutation (bidirectionaliterator first,
Bidirectionaliterator last );

Template <class bidirectionaliterator, class compare>
Bool next_permutation (bidirectionaliterator first,
Bidirectionaliterator last, compare comp );
Example:

// Next_permutation # include <iostream> # include <algorithm> using namespace STD; int main () {int myints [] = {1, 2, 3}; cout <"the 3! Possible permutations with 3 elements: \ n "; sort (myints, myints + 3 ); do {cout <myints [0] <"" <myints [1] <"" <myints [2] <Endl;} while (next_permutation (myints, myints + 3); Return 0;} 6. prev_permutation: similar to next_permutation, a prototype of the combination before (all elements) in the sequence is returned: <algorithm>
template <class BidirectionalIterator>  bool prev_permutation (BidirectionalIterator first,                         BidirectionalIterator last );template <class BidirectionalIterator, class Compare>  bool prev_permutation (BidirectionalIterator first,                         BidirectionalIterator last, Compare comp);
Example:
// Prev_permutation # include <iostream> # include <algorithm> using namespace STD; int main () {int myints [] = {1, 2, 3}; cout <"the 3! Possible permutations with 3 elements: \ n "; sort (myints, myints + 3); reverse (myints, myints + 3 ); do {cout <myints [0] <"" <myints [1] <"" <myints [2] <Endl;} while (prev_permutation (myints, myints + 3); Return 0;} 7. lexicographical_compare: dictionary comparison (for two sequences, return a Boolean value) prototype: Template <class inputiterator1, class inputiterator2> bool lexicographical_compare (inputiterator1 first1, inputiterator1 last1,
                                 InputIterator2 first2, InputIterator2 last2 );
template <class InputIterator1, class InputIterator2, class Compare>
  bool lexicographical_compare ( InputIterator1 first1, InputIterator1 last1,
                                 InputIterator2 first2, InputIterator2 last2,
Compare comp); example: // lexicographical_compare example # include <iostream> # include <algorithm> # include <cctype> using namespace STD; // a case-insensitive comparison function: bool mycomp (char C1, char C2) {return tolower (C1) <tolower (C2);} int main () {char first [] = "apple "; // 5 letters char second [] = "apartment"; // 9 letters cout <"using default comparison (operator <):"; if (lexicographical_compare (first, first + 5, second, second + 9) cout <first <"is less than" <second <Endl; else if (lexicographical_compare (second, second + 9, first, first + 5) cout <first <"is greater than" <second <Endl; else cout <first <"and" <second <"are equivalent \ n"; cout <"using mycomp as comparison object:"; if (lexicographical_compare (first, first + 5, second, second + 9, mycomp) cout <first <"is less than" <second <Endl; else if (lexicographical_compare (second, second + 9, first, first + 5, mycomp) cout <first <"is greater than" <second <Endl; else cout <first <"and" <second <"are equivalent \ n"; return 0 ;}

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.