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
Name | Description |
---|---|
|
|
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; | |
|
|
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; | |
| Returns a pair object containing lowest and highest values.
|
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>()); | |
| Returns an iterator.
|
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>()); | |
| Returns an iterator.
|
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>()); | |
| Returns a pair object having iterators for the elements containing lowest and highest values.
|
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
Name | Description | github | wandbox |
---|---|---|---|
Example | Usage | source output | source + output |
No comments:
Post a Comment