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

    Standard Library min/max AlgorithmsStandard Library min/max Algorithmsmin/max

    min min

    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 const& p1, P const& p2){ return p1.q < p2.q; }); // P pmin {1,'y'};

    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

    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 const& p1, P const& p2){ return p1.q < p2.q; }); // P pmax {2,'x'};

    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 const& p1, P const& p2){ return p1.q < p2.q; }); // P pmax {3,'a'}

    minmax minmax

    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

    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::minmax(a,b);
    
    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 const& p1, P const& p2){ return p1.q < p2.q; }); // P lo {1,'c'}, hi {3,'a'}

    clamp clamp

    clamps value in the interval given by hi and lo; 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

    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'

    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

    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 argmin = distance(begin(v), j); // int argmin = 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'

    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

    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
    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 cout << *i <<' '<< *j; // 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

    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