Beginner's Guide
    First Steps
    Input & Output
    Basic Custom Types
    Diagnostics
    Standard Library
    Code Organization
    Powerful Custom Types
    Generic Programming
    Memory Management
    Software Design Basics

    Standard min/max Algorithmsmin/max Algorithmsmin/max

    contains two values of different or same type

    std::pair<int,double> p {4, 8.15};
    
    std::cout << p.first  <<'\n';   // 4
    std::cout << p.second <<'\n';   // 8.15
    
    //  type deduction:
    std::pair p2 {4, 8.15};   // std::pair<int,double>

    Non-Range min/max/clamp min/max/clamp Non-Range

    std::min min

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

    ({v1,v2,v3, …}) →  smallest value

    (a, b, f(o,o)→bool) →  a if f(a,b) is true, b otherwise

    int a = 2;
    int b = 9;
    int x = std::min(a,b);  // x: 2
    int c = 0;
    int y = std::min({a,3,4,b,c,5});  // y: 0

    std::max max

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

    ({v1,v2,v3,…}) →  largest value

    (a, b, f(o,o)→bool) →  b if f(a,b) is true, a otherwise

    int a = 2;
    int b = 9;
    int x = std::max(a,b);  // x: 9
    int d = 11;
    int y = std::max({a,3,4,b,0,d});  // y: 11

    std::minmax minmax

    (a, b) →  pair{ smallest , largest }

    ({v1,v2,v3, …}) →  pair{ smallest , largest }

    (a, b, f(o,o)→bool) →  pair{ smallest , largest }

    comparison function/object f(a,b) must return true if a is less than b

    int a = 2;
    int b = 9;
    auto x = std::minmax(a,b);  // x has type std::pair<int,int>
    auto min = x.first;  // min: 2
    auto max = x.second; // max: 9

    std::clamp clamp

    (value,hi,lo) → clamped_value

    (value,hi,lo,f(o,o)→bool) → clamped_value

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

    int a = 8;
    int b = -4;
    int ac = std::clamp(a, 1, 5);  // ac: 5
    int bc = std::clamp(b, 1, 5);  // bc: 1

    std::min_element min_element

    returns an iterator to the smallest element;
    the 2nd version uses 'compare' for comparing elements, while the 1st version uses operator<

    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
    // smallest in entire vector: auto j = min_element(begin(v), end(v)); std::cout << *j; // prints '0'
    // erase at i's position:
    i = v.erase(i);   
    
    std::cout << *i; // prints '4'
    • 7
    • 9
    • 3
    • 5
    • 3
    • 2
    • 4
    • 1
    • 8
    • 0

    • 7
    • 9
    • 3
    • 5
    • 3
    • 4
    • 1
    • 8
    • 0

    std::max_element max_element

    returns an iterator to the largest element;
    the 2nd version uses 'compare' for comparing elements, while the 1st version uses operator<

    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
    // largest in entire vector: auto j = max_element(begin(v), end(v)); std::cout << *j; // prints '9'
    // erase at i's position:
    i = v.erase(i);
    
    std::cout << *i; // prints '3'
    • 7
    • 9
    • 3
    • 5
    • 3
    • 2
    • 4
    • 1
    • 8
    • 0

    • 7
    • 3
    • 5
    • 3
    • 2
    • 4
    • 1
    • 8
    • 0

    std::minmax_element
    minmax_element

    returns a pair of iterators to both the smallest and largest elements;
    the 2nd version uses 'comp' for comparing elements, while the 1st version uses operator<

    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)+9); auto min = *p.first; // int min = 2 auto max = *p.second; // int max = 8
    // 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