首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::minmax

Defined in header <algorithm>

(1)

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

(since C++11) (until C++14)

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

(since C++14)

(2)

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

(since C++11) (until C++14)

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

(since C++14)

(3)

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

(since C++11) (until C++14)

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

(since C++14)

(4)

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

(since C++11) (until C++14)

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

(since C++14)

返回给定值中的最低值和最大值。

1-2%29返回对小的和大的ab...

3-4%29返回初始化程序列表中最小和最大的值。ilist...

%281,3%29版本使用operator<比较值,而%282,4%29版本使用给定的比较函数comp...

参数

a, b

-

the values to compare

ilist

-

initializer list with the values to compare

comp

-

comparison function object (i.e. an object that satisfies the requirements of Compare) which returns ​true if the first argument is less than the second. The signature of the comparison function should be equivalent to the following: bool cmp(const Type1 &a, const Type2 &b); The signature does not need to have const &, but the function object must not modify the objects passed to it. The types Type1 and Type2 must be such that an object of type T can be implicitly converted to both of them. ​

类型要求

-T必须符合LessThanable的要求,才能使用过载%281,3%29。

-T必须符合CopyConstrucable的要求,才能使用重载%283,4%29。

返回值

1-2%29返回std::pair<const T&, const T&>(a, b)如果a<b或者如果a等于b返回std::pair<const T&, const T&>(b, a)如果b<a...

最小值的一对ilist作为第一元素和最伟大的元素。如果多个元素等效于最小元素,则返回最左边的此类元素。如果多个元素等效于最大的元素,则返回最右边的此类元素。

复杂性

1-2%29正是一个比较

3-4%29ilist.size() * 3 / 2比较

可能的实施

第一版

*。

模板<class T>STD:配对<Const T&,Const T&>Minst T&>minmax%28 Const T&a,Const T&b%29{返回%28b<a%29?STD:对<Const T&,Const T&>%28b,a%29:STD::对<Const T&,Const T&>%28a,b%29;}

第二版

模板<类T,类比较>std::对<const T&,const T&>minmax%28 const T&a,const T&b,比较comp%29{comp%28 b,a%29?STD:对<Const T&,Const T&>%28b,a%29:STD::对<Const T&,Const T&>%28a,b%29;}

第三版

模板<class T>std::对<T,T>minmax%28 std::初始值[医]列单<T>ilist%29{autop=std::minmax[医]元素%28 ilist.start%28%29,ilist.end%28%29%29;返回std::make[医]配对%28%2A第一页,%2A第二页%29;}

第四版

模板<类T,类比较>std::偶对<T,T>minmax%28 std::初始化器[医]列单<T>比较Comp%29{autop=std::minmax[医]元素%28 ilist.start%28%29,ilist.end%28%29,comp%29;返回std::make[医]配对%28%2A第一页,%2A第二页%29;}

注记

对于重载%281,2%29,如果其中一个参数是rvalue,则返回的引用在包含调用minmax*

二次

代码语言:javascript
复制
int n = 1;
auto p = std::minmax(n, n+1);
int m = p.first; // ok
int x = p.second; // undefined behavior

二次

二次

代码语言:javascript
复制
#include <algorithm>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
 
int main()
{
    std::vector<int> v {3, 1, 4, 1, 5, 9, 2, 6}; 
    std::srand(std::time(0));
    std::pair<int, int> bounds = std::minmax(std::rand() % v.size(),
                                             std::rand() % v.size());
 
    std::cout << "v[" << bounds.first << "," << bounds.second << "]: ";
    for (int i = bounds.first; i < bounds.second; ++i) {
        std::cout << v[i] << ' ';
    }
    std::cout << '\n';
}

二次

可能的产出:

二次

代码语言:javascript
复制
v[2,7]: 4 1 5 9 2

二次

另见

min

returns the smaller of the given values (function template)

max

returns the greater of the given values (function template)

minmax_element (C++11)

returns the smallest and the largest elements in a range (function template)

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券