Beginner's Guide
    First Steps
    Input & Output
    Custom Types – Part 1
    Diagnostics
    Standard Library – Part 1
    Function Objects
    Standard Library – Part 2
    Code Organization
    Custom Types – Part 2
    Generic Programming
    Memory Management
    Software Design Basics

    Standard Library min/max Algorithms min/max Algorithms min/max

    min min

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

    (a, b, cmp(o,o)→bool) → a if cmp(a,b) is trueb otherwise

    int const a = 2;
    int const b = 9;
    int x = std::min(a,b);  // int x = 2
    
    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'};

    ({v1,v2,v3,…}) → smallest_valueC++11

    ({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!

    int const a = 2;
    int const b = 9;
    int x = std::min({3,4,b,3,a,8});  // int x = 2
    

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

    std::vector<int> v {7,9,3,5,3,1,4,8};
    auto x = std::ranges::min(v);  // int x = 1
    
    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'}

    max max

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

    (a, b, cmp(o,o)→bool) → a if cmp(a,b) is falseb otherwise

    int const a = 2;
    int const b = 9;
    int x = std::max(a,b);  // int x = 9
    
    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'};

    ({v1,v2,v3,…}) → largest_valueC++11

    ({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!

    int const a = 2;
    int const b = 9;
    int x = std::max({3,4,b,3,a,8});  // int x = 9
    

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

    std::vector<int> v {7,9,3,5,3,1,4,8};
    auto x = std::ranges::max(v);  // int x = 9
    
    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'}

    minmax minmax C++11

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

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

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

    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
    auto [lo,hi] = std::minmax(a,b);  // int lo = 2, hi = 9  C++17
    

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

    ({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!

    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
    auto [lo,hi] = std::minmax({3,0,b,3,a,8});  // int lo = 0, hi = 9  C++17
    

    returns a std::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 <

    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);
    
    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'}

    clamp clamp C++17

    (value, lo, hi) → clamped_value

    (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 <

    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
    

    min_element → @position min_element

    standard library algorithm 'min_element' visual example

    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' // index of smallest: auto argmin = distance(begin(v)j); // int argmin = 9
    // erase at i's pos:
    • 7
    • 9
    • 3
    • 5
    • 3
    • 2
    • 4
    • 1
    • 8
    • 0
    i = v.erase(i); //
    std::cout << *i; //
    • 7
    • 9
    • 3
    • 5
    • 3
    • 4
    • 1
    • 8
    • 0
    // prints '4'
    standard library algorithm 'min_element' visual example

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

    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
    

    max_element → @position max_element

    standard library algorithm 'max_element' visual example

    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' // index of largest: auto argmax = distance(begin(v)j); // int argmax = 1
    // erase at i's pos:
    • 7
    • 9
    • 3
    • 5
    • 3
    • 2
    • 4
    • 1
    • 8
    • 0
    i = v.erase(i); //
    std::cout << *i; //
    • 7
    • 3
    • 5
    • 3
    • 2
    • 4
    • 1
    • 8
    • 0
    // prints '3'
    standard library algorithm 'max_element' visual example

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

    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
    

    minmax_element → {@min,@max} minmax_element C++11

    standard library algorithm 'minmax_element' visual example

    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 <

    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 // swap element values
    std::swap(*p.first,*p.second);  
    • 7
    • 1
    • 3
    • 5
    • 3
    • 8
    • 6
    • 2
    • 9
    • 0

    // 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 auto [i,j] = minmax_element(begin(v), end(v)); C++17 std::cout << *i <<' '<< *j; // 0 9
    standard library algorithm 'minmax_element' visual example

    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

    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