Sunday, December 17, 2023

Algorithms - Min/Max

Overview
 Min/Max Algorithms  are template based functions that operate on various types of containers such as sequence, associative and unordered to perform actions such as finding the smallest and the largest element.
These function accepts parameters like 
  • iterators such as forward
  • initializer lists 
  • predicates such as  binary 
  • Unary values
The parameter type  Compare is a function that accepts two inputs and returns bool value (true or false). 
For example, bool isless(int i, int j) {return i < j;} or [](int i, int j) {return i < j;}

Details
NameDescription
  1. const T& min (const T& a, const T& b)
  2. const T& min (const T& a, const T& b, Compare comp)
  3. T min (initializer_list<T> ilist)
  4. T min (initializer_list<T> ilist, Compare comp)

  1. Returns the lesser of a and b.
  2. Returns a if comp(a,b) returns true else returns  b.
  3. Returns the lowest element of  the ilist
  4. Returns the element that returns true when compared with other elements in the list by calling comp.
Examples
//1
//prints 10
cout <<  min(10,20) << endl;

//2
//prints 20
cout << min(10,20,greater<int>()) << endl;

//3
//prints 10
cout <<  min({10,20}) << endl;

//4
//prints 20
cout << min({10,20},greater<int>()) << endl;
  1. const T& max (const T& a, const T& b)
  2. const T& max (const T& a, const T& b, Compare comp)
  3. T max (initializer_list<T> ilist)
  4. T max (initializer_list<T> ilist, Compare comp)

  1. Returns the greater of a and b.
  2. Returns a if comp(a,b) returns true else returns  b.
  3. Returns the highest element of  the ilist
  4. Returns the element that returns true when compared with other elements in the list by calling comp.
Example
//1
//prints 10
cout <<  min(10,20) << endl;

//2
//prints 20
cout << min(10,20,greater<int>()) << endl;

//3
//prints 10
cout <<  min({10,20}) << endl;

//4
//prints 20
cout << min({10,20},greater<int>()) << endl;
  1. pair<const T&, const T&> minmax (const T& a, const T& b)
  2. pair<const T&, const T&> minmax (
    const T& a, const T& b, Compare comp)
  3. pair<TT> minmax (initializer_list<T> ilist)
  4. pair<TT> minmax (initializer_list<T> ilist, Compare comp)
Returns a pair object containing lowest and highest values.
  1. Returns the lowest and highest of a and b.
  2. Returns a,b if comp(a,b) returns true else returns  b,a.
  3. Returns the lowest and highest elements of  the ilist
  4. For the lowest, selects the element for which  comp returns false when compared with other elements in the list. Similarly for the highest, selects the element for which  comp returns true when compared with other elements in the list.
Example
//1
//p:{10,20}
auto p =  minmax(10,20);
    
//2
//p2:{20,10}
auto p2 =  minmax(10,20,greater<int>());

//3
//p3:{10,20}
auto p3 = minmax({10,20});
    
//4
//p4:{20,10}
auto p4 = minmax({10,20},greater<int>());
  1. ForwardIterator min_element(
    ForwardIterator first, ForwardIterator last)
  2. ForwardIterator min_element(
    ForwardIterator first, ForwardIterator last, Compare comp)


Returns an iterator.
  1. Return smallest element in range [first,last]. 
  2. Selects the element for which  comp returns false when compared with other elements in the range [first,last].
Examples
auto il={10,20};
//1
//itr = begin(il)
auto itr =  min_element(begin(il),end(il));
    
//2
//itr2 = begin(il)+1
auto itr2 =  min_element(begin(il),end(il),greater<int>());
  1. ForwardIterator max_element (
    ForwardIterator first, ForwardIterator last)
  2. ForwardIterator max_element (
    ForwardIterator first, ForwardIterator lastCompare comp)

Returns an iterator.
  1. Return highest element in range [first,last]. 
  2. Selects the element for which  comp returns true when compared with other elements in the range [first,last].
Examples
auto il={10,20};
//1
//itr = begin(il)+1
auto itr =  max_element(begin(il),end(il));
    
//2
//itr2 = begin(il)
auto itr2 =  max_element(begin(il),end(il),greater<int>());
  1. pair<ForwardIteratorForwardIterator minmax_element(
    ForwardIterator first, ForwardIterator last)
  2. pair<ForwardIteratorForwardIterator minmax_element (
    ForwardIterator first, ForwardIterator last, Compare comp)
Returns a pair object having iterators for the elements containing lowest and highest values.
  1. Returns the lowest and highest elements of  the range [first,last]. 
  2. For the lowest, selects the element for which  comp returns false when compared with other elements in the range [first,last].  Similarly for the highest, selects the element for which  comp returns true when compared with other elements in the range [first,last]. 
Examples
auto il={10,20};
//1
//p:{begin(il),begin(il)+1}
auto p =  minmax_element(begin(il),end(il));
    
//2
//p2:{begin(il)+1,begin(il)}
auto p2 =  minmax_element(begin(il),end(il),greater<int>());

The example depicts the usage.

Summary of Examples
NameDescriptiongithubwandbox
  Example       Usage   source    output      source + output   





No comments:

Post a Comment