定义于头文件 <algorithm>
算法库提供大量用途的函数(例如查找、排序、计数、操作),它们在元素范围上操作。注意范围定义为 [first, last) ,其中 last 指代要查询或修改的最后元素的后一个元素。
返回各给定值中的较小者
std::min| template< class T > | (1) | (C++14 前) | 
| template< class T > | (C++14 起) | |
| template< class T, class Compare > | (2) | (C++14 前) | 
| template< class T, class Compare > | (C++14 起) | |
| template< class T > | (3) | (C++11 起) (C++14 前) | 
| template< class T > | (C++14 起) | |
| template< class T, class Compare > | (4) | (C++11 起) (C++14 前) | 
| template< class T, class Compare > | (C++14 起) | 
返回给定值中的较小者。
1-2) 返回 a 与 b 的较小者。
3-4) 返回 initializer_list ilist 中值的最小者。
(1,3) 版本用 operator< 比较元素, (2,4) 版本用给定的比较函数 comp 。
参数
| a, b | - | 要比较的值 | 
| ilist | - | 拥有要比较的值的 initializer_list | 
| cmp | - | 比较函数对象(即满足比较 (Compare) 要求的对象),若 a小于b,则返回 true 。比较函数的签名应等价于如下: bool cmp(const Type1 &a, const Type2 &b); 虽然签名不必有 const & ,函数也不能修改传递给它的对象,而且必须接受(可为 const 的)类型  | 
| 类型要求 | ||
| - 为使用重载 (1,3) , T必须满足可小于比较 (LessThanComparable) 的要求。 | ||
| - 为使用重载 (3,4) , T必须满足可复制构造 (CopyConstructible) 的要求。 | ||
返回值
1-2) a 与 b 的较小者。若值等价,则返回 a 。
3-4) ilist 中的最小值。若有数个等价于最小者的值,则返回最左侧的这种值。
复杂度
1-2) 准确一次比较
3-4) 准确 ilist.size() - 1 次比较
可能的实现
版本一
template<class T> 
const T& min(const T& a, const T& b)
{
    return (b < a) ? b : a;
}版本二
template<class T, class Compare> 
const T& min(const T& a, const T& b, Compare comp)
{
    return (comp(b, a)) ? b : a;
}版本三
template<class T>
T min( std::initializer_list<T> ilist)
{
    return *std::min_element(ilist.begin(), ilist.end());
}版本四
template<class T, class Compare>
T min(std::initializer_list<T> ilist, Compare comp)
{
    return *std::min_element(ilist.begin(), ilist.end(), comp);
}警告
若参数之一是右值,且返回该参数,则以引用捕获 std::min 的结果会产生一个悬垂引用:
int n = 1;
const int& r = std::min(n-1, n+1);
// r 为悬垂调用示例
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <iterator>
#include <time.h>
using namespace std;
struct Cell
{
    int x;
    int y;
    Cell &operator +=(const Cell &cell)
    {
        x += cell.x;
        y += cell.y;
        return *this;
    }
    bool operator <(const Cell &cell) const
    {
        if (x == cell.x)
        {
            return y < cell.y;
        }
        else
        {
            return x < cell.x;
        }
    }
};
std::ostream &operator<<(std::ostream &os, const Cell &cell)
{
    os << "{" << cell.x << "," << cell.y << "}";
    return os;
}
int main()
{
    srand((unsigned)time(NULL));;
    std::cout << std::boolalpha;
    auto func1 = []()
    {
        int n = std::rand() % 10 + 100;
        Cell cell{n, n};
        return cell;
    };
    // 初始化cells1
    vector<Cell> cells1(6);
    std::generate(cells1.begin(), cells1.end(), func1);
    // 打印cells1
    std::copy(cells1.begin(), cells1.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;
    // 获取队列中最大都元素
    Cell tCell{999, 999};
    for (const Cell &cell : cells1)
    {
        tCell = std::min(tCell, cell);
        std::cout << tCell << " ";
    }
    std::cout << std::endl;
    std::cout << std::endl;
    std::cout << "min:      " << tCell << std::endl;
    std::cout << std::endl;
    std::cout << std::endl;
    auto larger = [](const Cell & a, const Cell & b)
    {
        if (a.x == b.x)
        {
            return a.y < b.y;
        }
        else
        {
            return a.x < b.x;
        }
    };
    // 初始化cells2
    vector<Cell> cells2(6);
    std::generate(cells2.begin(), cells2.end(), func1);
    // 打印cells2
    std::copy(cells2.begin(), cells2.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;
    // 获取队列中最大都元素
    Cell tCell2 {999, 999};
    for (const Cell &cell : cells2)
    {
        tCell2 = std::min(tCell2, cell, larger);
        std::cout << tCell << " ";
    }
    std::cout << std::endl;
    std::cout << std::endl;
    std::cout << "min:      " << tCell2 << std::endl;
    std::cout << std::endl;
    //返回 initializer_list ilist 中值的最大者。
    Cell tCell3 = std::min({Cell{1, 2}, Cell{3, 4}, Cell{5, 6}, Cell{7, 8}});
    std::cout << "initializer_list min:      " << tCell3 << std::endl;
    std::cout << std::endl;
    Cell tCell4 = std::min({Cell{1, 2}, Cell{3, 4}, Cell{5, 6}, Cell{7, 8}}, larger);
    std::cout << "initializer_list min:      " << tCell4 << std::endl;
    std::cout << std::endl;
    return 0;
}
输出


















![[附源码]Python计算机毕业设计高校教务管理系统Django(程序+LW)](https://img-blog.csdnimg.cn/2f8537bff50a4b24afd61e2ca8e6d550.png)

