Standard Library min/max Algorithms min/max Algorithms min/max
New to C++'s standard library algorithms? ⇒ Short Introduction
#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';
}
#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';
}
#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';
}
#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';
}
#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';
}
#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';
}
#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';
}
#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';
}
#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";
}
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';
}
#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: - 7
- 9
- 3
- 5
- 3
- 2
- 4
- 1
- 8
- 0
-
i = v.erase(i); // -
-
-
-
-
-
-
-
-
-
std::cout << *i << '\n'; // - 7
- 9
- 3
- 5
- 3
- 4
- 1
- 8
- 0
-
// prints '4' -
-
-
-
-
-
-
-
}
#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';
}
#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: - 7
- 9
- 3
- 5
- 3
- 2
- 4
- 1
- 8
- 0
-
i = v.erase(i); // -
-
-
-
-
-
-
-
-
-
std::cout << *i << '\n'; // - 7
- 9
- 3
- 3
- 2
- 4
- 1
- 8
- 0
-
// prints '3' -
-
-
-
-
-
-
-
-
}
#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';
}
#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); // ⇒ - 7
- 1
- 3
- 5
- 3
- 8
- 6
- 2
- 9
- 0
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
}
#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';
}
Comments…