Beginner's Guide

# Standard Library min/max Algorithmsmin/max Algorithmsmin/max

## `min`min

`min(a, b)` → a if (a < b) is `true`b otherwise

`min(a, b, cmp(o,o)→bool)` → a if `cmp(a,b)` is `true`b otherwise

``````#include <iostream>
#include <algorithm>

int main () {int const a = 2;
int const b = 9;
int x = std::min(a,b);  // int x = 2
std::cout << x << '\n';struct P { int q; char c; };
P pmin = std::min(P{1,'y'}, P{2,'x'},
[](P p1, P p2){ return p1.q < p2.q; });  // P pmin {1,'y'};
std::cout << pmin.q <<' '<< pmin.c << '\n';
}``````

`min({v1,v2,v3,…})` → `smallest_value`C++11

`min({v1,v2,v3,…}, cmp(o,o)→bool)` → `smallest_value`

the 2nd version uses `cmp` for comparing elements,
while the 1st version uses `operator <`

All elements in the input list `{ … }` must have the same type!

``````#include <iostream>
#include <algorithm>

int main () {int const a = 2;
int const b = 9;
int x = std::min({3,4,b,3,a,8});  // int x = 2
std::cout << x << '\n';
}``````

returns (a const reference to) the smallest element in `range`;
the 2nd version uses `cmp` for comparing elements, while the 1st version uses `operator <`

``````#include <iostream>
#include <algorithm>
#include <vector>

int main () {std::vector<int> v {7,9,3,5,3,1,4,8};
auto x = std::ranges::min(v);  // int x = 1
std::cout << x << '\n';struct P { int q; char c; };
std::vector<P> const w {P{3,'a'},P{1,'c'},P{2,'b'}};
auto pmin = std::ranges::min(w,
[](P const& p1, P const& p2){   return p1.q < p2.q; });  // P pmin {1,'c'}
std::cout << pmin.q <<' '<< pmin.c << '\n';
}``````

## `max`max

`max(a, b)` → a if (a < b) is `false`b otherwise

`max(a, b, cmp(o,o)→bool)` → a if `cmp(a,b)` is `false`b otherwise

``````#include <iostream>
#include <algorithm>

int main () {int const a = 2;
int const b = 9;
int x = std::max(a,b);  // int x = 9
std::cout << x << '\n';struct P { int q; char c; };
P pmax = std::max(P{1,'y'}, P{2,'x'},
[](P p1, P p2){ return p1.q < p2.q; });  // P pmax {2,'x'};
std::cout << pmax.q <<' '<< pmax.c << '\n';
}``````

`max({v1,v2,v3,…})` → `largest_value`C++11

`max({v1,v2,v3,…}, cmp(o,o)→bool)` → `largest_value`

the 2nd version uses `cmp` for comparing elements,
while the 1st version uses `operator <`

All elements in the input list `{ … }` must have the same type!

``````#include <iostream>
#include <algorithm>

int main () {int const a = 2;
int const b = 9;
int x = std::max({3,4,b,3,a,8});  // int x = 9
std::cout << x << '\n';
}``````

returns (a const reference to) the largest element in `range`;
the 2nd version uses `cmp` for comparing elements, while the 1st version uses `operator <`

``````#include <iostream>
#include <algorithm>
#include <vector>

int main () {std::vector<int> v {7,9,3,5,3,1,4,8};
auto x = std::ranges::max(v);  // int x = 9
std::cout << x << '\n';struct P { int q; char c; };
std::vector<P> const w {P{1,'c'},P{3,'a'},P{2,'b'}};
auto pmax = std::ranges::max(w,
[](P p1, P p2){   return p1.q < p2.q; });  // P pmax {3,'a'}
std::cout << pmax.q <<' '<< pmax.c << '\n';
}``````

## `minmax`minmaxC++11

`minmax(a, b)` → `{smallest,largest}`C++11

`minmax(a, b, cmp(o,o)→bool)` → `{smallest,largest}`

comparison function/object `cmp(a,b)` must return `true` if `a` should be ordered before `b`

``````#include <iostream>
#include <algorithm>
#include <utility>

int main () {int a = 2;
int b = 9;
auto p = std::minmax(a,b);  // std::pair<int,int> p {2,9}
auto min = p.first;  // int min = 2
auto max = p.second; // int max = 9
std::cout << min <<' '<< max << '\n';
auto [lo,hi] = std::minmax(a,b);  // int lo = 2, hi = 9  C++17
std::cout << lo <<' '<< hi << '\n';
}``````

`minmax({v1,v2,v3,…})` → `{smallest,largest}`C++11

`minmax({v1,v2,v3,…}, cmp(o,o)→bool)` → `{smallest,largest}`

the 2nd version uses `cmp` for comparing elements,
while the 1st version uses `operator <`

All elements in the input list `{ … }` must have the same type!

``````#include <iostream>
#include <algorithm>
#include <utility>

int main () {int const a = 2;
int const b = 9;
auto p = std::minmax({3,0,b,3,a,8});  // std::pair<int,int> p {0,9}
auto min = p.first;  // int min = 0
auto max = p.second; // int max = 9
std::cout << min <<' '<< max << '\n';
auto [lo,hi] = std::minmax({3,0,b,3,a,8});  // int lo = 0, hi = 9  C++17
std::cout << lo <<' '<< hi << '\n';
}``````

returns a pair of (const references to) the smallest and largest elements in `range`;
the 2nd version uses `cmp` for comparing elements, while the 1st version uses `operator <`

``````#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>

int main () {std::vector<int> v {7,9,3,5,3,1,4,8};
auto p = std::ranges::minmax(v);  // std::pair<int,int> p {1,9}
auto [min,max] = std::ranges::minmax(v);
std::cout << min <<' '<< max << '\n';
struct P { int q; char c; };
std::vector<P> const w {P{3,'a'},P{2,'b'},P{1,'c'}};
auto [lo,hi] = std::ranges::minmax(w,
[](P p1, P p2){   return p1.q < p2.q; });  // P lo {1,'c'}, hi {3,'a'}
std::cout << "(" << lo.q <<','<< lo.c << ")\n";
std::cout << "(" << hi.q <<','<< hi.c << ")\n";
}``````

## `clamp`clampC++17

`clamp(value, lo, hi) → clamped_value`

`clamp(value, lo, hi, cmp(o,o)→bool) → clamped_value`

clamps `value` in the interval given by `lo` and `hi`;
the second version uses `cmp` to compare values instead of `operator <`

``````#include <algorithm>
#include <iostream>

int main () {int a = std::clamp( 8,  1, 5);  // int a =  5
int b = std::clamp(-4,  1, 5);  // int b =  1
int c = std::clamp(-4, -2, 5);  // int c = -2
std::cout << a <<' '<< b <<' '<< c << '\n';
}``````

## `min_element → @position`min_element

the 2nd version uses `compare` for comparing elements,
while the 1st version uses `operator <`

``````#include <vector>
#include <iostream>
#include <algorithm>

int main () {std::vector<int> v {7,9,3,5,3,2,4,1,8,0};
// smallest in subrange (as shown in image):
auto i = min_element(begin(v)+2, begin(v)+7);
auto min = *i;  // int min = 2
std::cout << "min: " << min << '\n';
// smallest in entire vector:
auto j = min_element(begin(v), end(v));
std::cout << *j << '\n';  // prints '0'
// index of smallest:
auto argmin = distance(begin(v), j);  // int argmin = 9
std::cout << "argmin: " << argmin << '\n';
// erase at i's pos:  7935324180
i = v.erase(i);    //
std::cout << *i << '\n';   // 793534180
// prints '4'
}``````

uses `operator <` for comparing elements; alternatively a custom function(object) `comp` can be passed as second argument

``````#include <iostream>
#include <vector>
#include <algorithm>

int main () {std::vector<int> v {7,9,3,5,3,2,4,1,8,0};
auto i = std::ranges::min_element(v);
auto min = *i;  // int min = 0
std::cout << min << '\n';
}``````

## `max_element → @position`max_element

the 2nd version uses `compare` for comparing elements,
while the 1st version uses `operator <`

``````#include <vector>
#include <iostream>
#include <algorithm>

int main () {std::vector<int> v {7,9,3,5,3,2,4,1,8,0};
// largest in subrange (as shown in image):
auto i = max_element(begin(v)+2, begin(v)+7);
auto max = *i;  // int max = 5
std::cout << "max: " << max << '\n';
// largest in entire vector:
auto j = max_element(begin(v), end(v));
std::cout << *j << '\n';  // prints '9'
// index of largest:
auto argmax = distance(begin(v), j);  // int argmax = 1
std::cout << "argmax: " << argmax << '\n';
// erase at i's pos:  7935324180
i = v.erase(i);    //
std::cout << *i << '\n';   // 793324180
// prints '3'
}``````

uses `operator <` for comparing elements; alternatively a custom function(object) `comp` can be passed as second argument

``````#include <iostream>
#include <vector>
#include <algorithm>

int main () {std::vector<int> v {7,9,3,5,3,2,4,1,8,0};
auto i = std::ranges::max_element(v);
auto max = *i;  // int max = 9
std::cout << max << '\n';
}``````

## `minmax_element → {@min,@max}`minmax_elementC++11

returns a `std::pair` of iterators to the smallest and largest elements in the input range;

the 2nd version uses `comp` for comparing elements,
while the 1st version uses `operator <`

``````#include <vector>
#include <algorithm>
#include <iostream>

int main () {std::vector<int> v {7,1,3,5,3,8,6,2,9,0};
// min & max in subrange (as shown in image):
auto p = minmax_element(begin(v)+2, begin(v)+8);
auto min = *p.first;    // int min = 2
auto max = *p.second;   // int max = 8
std::cout << min <<' '<< max << '\n';
// swap element values
std::swap(*p.first,*p.second); // ⇒ 7135386290
std::cout << *p.first <<' '<< *p.second << '\n';
// min & max in entire vector:
auto pv = minmax_element(begin(v), end(v));
auto minv = *pv.first;    // int minv = 0
auto maxv = *pv.second;   // int maxv = 9
std::cout << minv <<' '<< maxv << '\n';
auto [i,j] = minmax_element(begin(v), end(v));  // C++17
std::cout << *i <<' '<< *j << '\n';  // 0 9
}``````

returns a `std::pair` of iterators to the smallest and largest elements in the input range;

uses `operator <` for comparing elements; alternatively a custom function(object) `cmp` can be passed as second argument

``````#include <iostream>
#include <vector>
#include <algorithm>

int main () {std::vector<int> v {7,1,3,5,3,8,6,2,9,0};
auto [i,j] = std::ranges::minmax_element(v);
auto min = *i;  // int min = 0
auto max = *j;  // int max = 9
std::cout << min <<' '<< max << '\n';
}``````