CodeNebula
C++20stladvanced

std::ranges::min / max / minmax (C++20 Addition)

C++20 · The Big Four · Compiler flag: -std=c++20

Explanation

Return the minimum, maximum, or both from a range or initializer list. Support projections and custom comparators.

Real-World Scenario

Find the cheapest and most expensive items in a product catalog in a single pass.

Quick Snippet

snippet.cpp
#include <ranges>
#include <algorithm>
#include <vector>

struct Product { std::string name; double price; };
std::vector<Product> catalog = {{"A", 9.99}, {"B", 4.99}, {"C", 14.99}};

auto [cheapest, priciest] = std::ranges::minmax(catalog, {}, &Product::price);
// cheapest.name == "B", priciest.name == "C"

Full Example

ranges_min_max.cpp
// CodeNebula - C++20 Example: Ranges Min Max
// Compile: g++ -std=c++20 -Wall -Wextra -o example ranges_min_max.cpp

#include <algorithm>
#include <iostream>
#include <vector>
#include <string>

int main() {
    // ranges::min - return the smaller of two values
    std::cout << "min(3, 7): " << std::ranges::min(3, 7) << '\n';
    std::cout << "min(5, 5): " << std::ranges::min(5, 5) << '\n';

    // ranges::max - return the larger of two values
    std::cout << "max(3, 7): " << std::ranges::max(3, 7) << '\n';

    // ranges::min / max with initializer list
    int smallest = std::ranges::min({5, 2, 8, 1, 9, 3});
    int largest  = std::ranges::max({5, 2, 8, 1, 9, 3});
    std::cout << "\nmin({5,2,8,1,9,3}): " << smallest << '\n';
    std::cout << "max({5,2,8,1,9,3}): " << largest << '\n';

    // ranges::min / max over a range
    std::vector<int> nums = {42, 17, 93, 5, 68};
    std::cout << "\nVector min: " << std::ranges::min(nums) << '\n';
    std::cout << "Vector max: " << std::ranges::max(nums) << '\n';

    // ranges::minmax - get both min and max at once
    auto [lo, hi] = std::ranges::minmax(nums);
    std::cout << "\nminmax: [" << lo << ", " << hi << "]\n";

    auto [lo2, hi2] = std::ranges::minmax({10, 30, 20, 50, 40});
    std::cout << "minmax({10,30,20,50,40}): [" << lo2 << ", " << hi2 << "]\n";

    // With custom comparator
    std::cout << "\nmin by abs: "
              << std::ranges::min(-5, 3, [](int a, int b) {
                     return std::abs(a) < std::abs(b);
                 }) << '\n';

    // With projection
    std::vector<std::string> words = {"banana", "fig", "cherry", "apple"};
    auto shortest = std::ranges::min(words, {}, &std::string::size);
    auto longest  = std::ranges::max(words, {}, &std::string::size);
    std::cout << "\nShortest word: " << shortest << '\n';
    std::cout << "Longest word:  " << longest << '\n';

    auto [s, l] = std::ranges::minmax(words, {}, &std::string::size);
    std::cout << "minmax by length: [" << s << ", " << l << "]\n";

    return 0;
}
Run command
g++ -std=c++20 -Wall -Wextra -o example ranges_min_max.cpp