C++11标准模板(STL)- 算法 - 最小/最大操作(std::minmax)

定义于头文件 <algorithm>

 

算法库提供大量用途的函数(例如查找、排序、计数、操作),它们在元素范围上操作。注意范围定义为 [first, last) ,其中 last 指代要查询或修改的最后元素的后一个元素。

返回两个元素的较小和较大者

std::minmax

template< class T >
std::pair<const T&,const T&> minmax( const T& a, const T& b );

(1)(C++11 起)
(C++14 前)

template< class T >
constexpr std::pair<const T&,const T&> minmax( const T& a, const T& b );

(C++14 起)
template< class T, class Compare >

std::pair<const T&,const T&> minmax( const T& a, const T& b,

                                     Compare comp );
(2)(C++11 起)
(C++14 前)
template< class T, class Compare >

constexpr std::pair<const T&,const T&> minmax( const T& a, const T& b,

                                               Compare comp );
(C++14 起)

template< class T >
std::pair<T,T> minmax( std::initializer_list<T> ilist);

(3)(C++11 起)
(C++14 前)

template< class T >
constexpr std::pair<T,T> minmax( std::initializer_list<T> ilist);

(C++14 起)

template< class T, class Compare >
std::pair<T,T> minmax( std::initializer_list<T> ilist, Compare comp );

(4)(C++11 起)
(C++14 前)

template< class T, class Compare >
constexpr std::pair<T,T> minmax( std::initializer_list<T> ilist, Compare comp );

(C++14 起)

返回给定值的最小和最大者。

1-2) 返回到 ab 较小和较大者的引用。

3-4) 返回 initializer_list ilist 中值的最小和最大者。

(1,3) 版本用 operator< 比较值,而 (2,4) 版本用给定的比较函数 comp

参数

a, b-要比较的值
ilist-含要比较的值的 initializer_list
comp-比较函数对象(即满足比较 (Compare) 要求的对象),若首个参数小于第二个,则返回 ​true 。

比较函数的签名应等价于如下:

 bool cmp(const Type1 &a, const Type2 &b);

虽然签名不必有 const & ,函数也不能修改传递给它的对象,而且必须接受(可为 const 的)类型 Type1Type2 的值,无关乎值类别(从而不允许 Type1 & ,亦不允许 Type1 ,除非 Type1 的移动等价于复制 (C++11 起))。
类型 Type1 与 Type2 必须使得 T 类型的对象能隐式转换到这两个类型。 ​

类型要求
- 为使用重载 (1,3) , T 必须满足可小于比较 (LessThanComparable) 的要求。
- 为使用重载 (3,4) , T 必须满足可复制构造 (CopyConstructible) 的要求。

返回值

1-2) 若 a<b 或若 a 等价于 b 则返回 std::pair<const T&, const T&>(a, b) 的结果。若 b<a 则返回 std::pair<const T&, const T&>(b, a) 的结果。

3-4) 以 ilist 中最小元素为第一元素,最大元素为第二元素的 pair 。若有数个元素等价于最小者,则返回最左的这种元素。若有数个元素等价于最大者,则返回最右的这种元素。

复杂度

1-2) 准确一次比较

3-4) 至多 ilist.size() * 3 / 2 次比较

可能的实现

版本一

template<class T> 
std::pair<const T&, const T&> minmax( const T& a, const T& b )
{
    return (b < a) ? std::pair<const T&, const T&>(b, a)
                   : std::pair<const T&, const T&>(a, b);
}

版本二

template<class T, class Compare> 
std::pair<const T&, const T&> minmax( const T& a, const T& b, Compare comp )
{
    return comp(b, a) ? std::pair<const T&, const T&>(b, a)
                      : std::pair<const T&, const T&>(a, b);
}

版本三

template< class T >
std::pair<T, T> minmax( std::initializer_list<T> ilist )
{
    auto p = std::minmax_element(ilist.begin(), ilist.end());
    return std::make_pair(*p.first, *p.second);
}

版本四

template< class T, class Compare >
std::pair<T, T> minmax( std::initializer_list<T> ilist, Compare comp )
{
    auto p = std::minmax_element(ilist.begin(), ilist.end(), comp);
    return std::make_pair(*p.first, *p.second);
}

注意

对于重载 (1,2) ,若参数之一为右值,则返回的引用在包含对 minmax 调用的完整表达式结尾变为悬垂引用:

int n = 1;
auto p = std::minmax(n, n+1);
int m = p.first; // ok
int x = p.second; // 未定义行为

调用示例

#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;
    };

    Cell cell_1 = func1();
    std::cout << "cell_1:       " << cell_1 << std::endl;
    Cell cell_2 = func1();
    std::cout << "cell_2:       " << cell_2 << std::endl;

    std::pair<const Cell, const Cell> bounds = std::minmax(cell_1, cell_2);
    std::cout << "min:          " << bounds.first  << std::endl;
    std::cout << "max:          " << bounds.second << 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;
        }
    };

    Cell cell_3 = func1();
    std::cout << "cell_3:       " << cell_3 << std::endl;
    Cell cell_4 = func1();
    std::cout << "cell_4:       " << cell_4 << std::endl;

    std::pair<const Cell, const Cell> bounds2 = std::minmax(cell_3, cell_4, larger);
    std::cout << "min:          " << bounds2.first  << std::endl;
    std::cout << "max:          " << bounds2.second << std::endl;

    std::cout << std::endl;

    std::pair<const Cell, const Cell> bounds3 = std::minmax({Cell{1, 2}, Cell{3, 4}, Cell{5, 6}, Cell{7, 8}});
    std::cout << "min:          " << bounds3.first  << std::endl;
    std::cout << "max:          " << bounds3.second << std::endl;

    std::cout << std::endl;

    std::pair<const Cell, const Cell> bounds4 = std::minmax({Cell{1, 2}, Cell{3, 4}, Cell{5, 6}, Cell{7, 8}}, larger);
    std::cout << "min:          " << bounds4.first  << std::endl;
    std::cout << "max:          " << bounds4.second << std::endl;

    return 0;
}

输出

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值