# Algorithms for determining the minimum and maximum. Example

### Contents

Search other resources:

##### 1. Algorithm min. Search for the minimum between two values

The min algorithm compares two objects and returns the smaller of the two. The criterion for comparing elements can be specified by a predicate.

The algorithm has several overloaded implementations

```template <class Type>
constexpr const Type& min(
const Type& left,
const Type& right);

template <class Type, class Pr>
constexpr const Type& min(
const Type& left,
const Type& right,
BinaryPredicate pred);

template <class Type>
constexpr Type min(
initializer_list<Type> ilist);

template <class Type, class Pr>
constexpr Type min(
initializer_list<Type> ilist,
BinaryPredicate pred);```

where

• left, right – compared objects;
• pred – a predicate used to compare two objects. It can also be a lambda expression;
• inlist – an object that contains elements to compare.

Example.

```#include <iostream>
#include <list>
#include <algorithm>
#include <vector>
#include <functional>
using namespace std;

// Comparison function.
// If the < sign is replaced by >, then there will be a search for the maximum.
bool MoreThan(int a, int b)
{
return a < b;
}

void main()
{
// The min algorithm is the search for the minimum between two values
// 1. Search between values of type double
double minDouble = min(2.8, 3.6);
cout << "minDouble = " << minDouble << endl;

// 2. Search between integers using the compare function
int minInt = min(29, 36, MoreThan);
cout << "minInt = " << minInt << endl;
}```

Result

```minDouble = 2.8
minInt = 29```

### ⇑

##### 2. Algorithm min_element. Search for the minimum element in a set of values

The algorithm searches for the smallest element in the given range. The ordering criterion can be specified by a binary predicate.

The most used overloaded implementations of the algorithm

```template <class ForwardIterator>
constexpr ForwardIterator min_element(
ForwardIterator first,
ForwardIterator last );

template <class ForwardIterator, class Compare>
constexpr ForwardIterator min_element(
ForwardIterator first,
ForwardIterator last,
Compare pred);```

where

• first, last – direct iterators pointing respectively to the first and last element of the range;
• pred – a binary predicate that specifies a condition when one element is less than another. If the first element is less than the other, then the predicate must return true.

Example.

```#include <iostream>
#include <list>
#include <algorithm>
#include <vector>
#include <functional>
using namespace std;

// Comparison function.
// If the < sign is replaced by >, then there will be a search for the maximum.
bool MoreThan(int a, int b)
{
return a < b;
}

void main()
{
// The min_element algorithm is a search for the minimum in a set of values.
// 1. Search in a vector of numbers of type double.
vector<double> V = { 2.8, 1.6, 2.9, 3.3, 4.5, -1.8, -2.2 };
vector<double>::iterator itV = min_element(V.begin(), V.end());
cout << "min_element(V) = " << *itV << endl;

// 2. Search between integers using the compare function
list<int> L = { 2, 3, 8, -4, 1, 3, -2 };
list<int>::iterator itL = min_element(L.begin(), L.end(), MoreThan);
cout << "min_element(L) = " << *itL << endl;

// 3. Search between integers, lambda expression is used
itL = min_element(L.begin(), L.end(), [](int a, int b) { return a < b; });
cout << "min_element(L) = " << *itL << endl;
}```

Result

```min_element(V) = -2.2
min_element(L) = -4
min_element(L) = -4```

### ⇑

##### 3. Algorithm max. Searching for the maximum between two values

The max algorithm compares two objects and returns the larger one. In the algorithm, it is allowed to set the ordering criterion using a binary predicate.

The overloaded implementations of the algorithm are as follows

```template <class Type>
constexpr Type& max(
const Type& left,
const Type& right);

template <class Type, class Pr>
constexpr Type& max(
const Type& left,
const Type& right,
BinaryPredicate pred);

template <class Type>
constexpr Type& max (
initializer_list<Type> ilist);

template <class Type, class Pr>
constexpr Type& max(
initializer_list<Type> ilist,
BinaryPredicate pred);```

where

• left, right – respectively the first and second of the compared objects;
• pred – a binary predicate that specifies how objects are compared;
• ilist – list with objects to be compared.

Example.

```#include <iostream>
#include <list>
#include <algorithm>
#include <vector>
#include <functional>
using namespace std;

// Comparison function.
// If the < sign is replaced by >, then there will be a search for a minimum.
bool LessThan(int a, int b)
{
return a < b;
}

void main()
{
// Algorithm max - searching for the maximum between two values
// 1. Searching between double values
double maxDouble = max(2.8, 3.6);
cout << "maxDouble = " << maxDouble << endl;

// 2. Search between integers using the compare function
int maxInt = max(29, 36, LessThan);
cout << "maxInt = " << maxInt << endl;
}```

Result

```maxDouble = 3.6
maxInt = 36```

### ⇑

##### 4. Algorithm max_element. Search for the maximum in a sequence

The max_element algorithm returns the first occurrence of the largest element in the sequence given by the range. In the algorithm, you can specify a binary predicate that specifies the search condition for the maximum value.

The algorithm has several overloaded implementations, the most common of which are:

```template <class ForwardIterator>
constexpr ForwardIterator max_element(
ForwardIterator first,
ForwardIterator last );

template <class ForwardIterator, class Compare>
constexpr ForwardIterator max_element(
ForwardIterator first,
ForwardIterator last,
Compare pred );```

where

• first, last – direct iterators that point respectively to the first and last element of the considered range;
• pred – a binary predicate that specifies a condition when one element is less than another. If the first element is less than the second, then the predicate must return true.

Example.

```#include <iostream>
#include <list>
#include <algorithm>
#include <vector>
#include <functional>
using namespace std;

// Comparison function.
// If the < sign is replaced by >, then there will be a search for the minimum.
bool MoreThan(int a, int b)
{
return a < b;
}

void main()
{
// Algorithm max_element - search for the maximum in a set of values.
// 1. Search in a vector of numbers of type double
vector<double> V = { 2.8, 1.6, 2.9, 3.3, 4.5, -1.8, -2.2 };
vector<double>::iterator itV = max_element(V.begin(), V.end());
cout << "max_element(V) = " << *itV << endl;

// 2. Search between integers, using the comparison function
list<int> L = { 2, 3, 8, -4, 1, 3, -2 };
list<int>::iterator itL = max_element(L.begin(), L.end(), MoreThan);
cout << "max_element(L) = " << *itL << endl;

// 3. Search between integers, lambda expression is used
itL = max_element(L.begin(), L.end(), [](int a, int b) { return a < b; });
cout << "min_element(L) = " << *itL << endl;
}```

Result

```max_element(V) = 4.5
max_element(L) = 8
min_element(L) = 8```

### ⇑

##### 5. The lexicographical_compare algorithm. Compare element by element 2 sequences

The algorithm performs an element-by-element comparison of two sequences to determine which element of the two sequences is smaller.

Common implementations of the algorithm are as follows

```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 pred );```

where

• first1, last1 – iterators that define the range of elements of the first sequence;
• first2, last2 – iterators that define the limits of the second sequence.

Element-by-element comparison of sequences occurs according to the following rules:

• the first 2 unequal corresponding elements are found and the result of their comparison is the result of comparing two sequences;
• if the elements of the sequences are equal, then the lengths of the sequences are compared. More is the sequence, the number of elements in which is greater;
• if the sequences are identical and have the same number of elements, then the result of the comparison is false.

Example.

```#include <iostream>
#include <queue>
#include <vector>
#include <list>
#include <functional>
#include <algorithm>
using namespace std;

int main()
{
// Algorithm lexicographical_compare - compares 2 sequences element by element,
// if the sequences are identical - the result is false.

// 1. Declare two vectors
vector<double> V1 = { 1.1, 2.2, 2.7, 2.8, 3.2, 5.5 };
vector<double> V2 = { 1.1, 2.2, 2.7, 2.8, 3.2, 5.5 };

// 2. Call the algorithm, get the result
bool res = lexicographical_compare(V1.begin(), V1.end(), V2.begin(), V2.end());

// 3. Process the result
if (res)
cout << "true => V1 != V2" << endl;
else
cout << "false => V1 == V2" << endl;
}```

Result

`false => V1 == V2`