文档章节

STL Algorithm 之 min&max

NiuYoohoo
 NiuYoohoo
发布于 2017/04/13 21:54
字数 897
阅读 6
收藏 0

 max

template<class Type>
   const Type& max(
      const Type& _Left, 
      const Type& _Right
   );
template<class Type, class Pr>
   const Type& max(
      const Type& _Left, 
      const Type& _Right,
      BinaryPredicate _Comp
   );

Parameters

_Left

The first of the two objects being compared.

_Right

The second of the two objects being compared.

_Comp

A binary predicate used to compare the two objects.

Return Value

The greater of the two objects unless neither is greater, in which case it returns the first of the two objects.

Remarks

The max algorithm is unusual in having objects passed as parameters. Most Standard Template Library algorithms operate on a range of elements whose position is specified by iterators passes as parameters.

max_element

template<class ForwardIterator>
   ForwardIterator max_element(
      ForwardIterator _First, 
      ForwardIterator _Last
   );
template<class ForwardIterator, class BinaryPredicate>
   ForwardIterator max_element(
      ForwardIterator _First, 
      ForwardIterator _Last, 
      BinaryPredicate _Comp
   );

Parameters

_First

A forward iterator addressing the position of the first element in the range to be searched for the largest element.

_Last

A forward iterator addressing the position one past the final element in the range to be searched for the largest element.

_Comp

User-defined predicate function object that defines the sense in which one element is greater than another. The binary predicate takes two arguments and should return true when the first element is less than the second element and false otherwise.

Return Value

A forward iterator addressing the position of the first occurrence of the largest element in the range searched.

Remarks

The range referenced must be valid; all pointers must be dereferenceable and within each sequence the last position is reachable from the first by incrementation.

The complexity is linear: (_Last – _First) – 1 comparisons are required for a nonempty range.

min

template<class Type>
   const Type& min(
      const Type& _Left, 
      const Type& _Right
   );
template<class Type, class Pr>
   const Type& min(
      const Type& _Left, 
      const Type& _Right,
      BinaryPredicate _Comp
   );

Parameters

_Left

The first of the two objects being compared.

_Right

The second of the two objects being compared.

_Comp

A binary predicate used to compare the two objects.

Return Value

The lesser of the two objects unless neither is lesser, in which case it returns the first of the two objects.

The min algorithm is unusual in having objects passed as parameters. Most Standard Template Library algorithms operate on a range of elements whose position is specified by iterators passed as parameters.

min_element


template<class ForwardIterator>
   ForwardIterator min_element(
      ForwardIterator _First, 
      ForwardIterator _Last
   );
template<class ForwardIterator, class BinaryPredicate>
   ForwardIterator min_element(
      ForwardIterator _First, 
      ForwardIterator _Last,
      BinaryPredicate _Comp
   );

Parameters

_First

A forward iterator addressing the position of the first element in the range to be searched for the largest element.

_Last

A forward iterator addressing the position one past the final element in the range to be searched for the largest element.

_Comp

User-defined predicate function object that defines the sense in which one element is greater than another. The binary predicate takes two arguments and should return true when the first element is less than the second element and false otherwise.

Return Value

A forward iterator addressing the position of the first occurrence of the smallest element in the range searched.

Remarks

The range referenced must be valid; all pointers must be dereferenceable and within each sequence the last position is reachable from the first by incrementation.

The complexity is linear: (_Last – _First) – 1 comparisons are required for a nonempty range.

minmax

template<class Type>
    pair<const Type&, const Type&>
        minmax(
            const Type& _Left, 
            const Type& _Right
        );
template<class Type, class BinaryPredicate>
    pair<const Type&, const Type&>
        minmax(
            const Type& _Left,
            const Type& _Right,
            BinaryPredicate _Comp
        );
template<class Type> 
    pair<Type&, Type&> 
        minmax(
            initializer_list<Type> _Ilist
        );
template<class Type, class BinaryPredicate> 
    pair<Type&, Type&> 
        minmax(
            initializer_list<Type> _Ilist, 
            BinaryPredicate _Comp
        );

Parameters

_Left

The first of the two objects being compared.

_Right

The second of the two objects being compared.

_Comp

A binary predicate used to compare the two objects.

_IList

The initializer_list that contains the members to be compared.

Return Value

Returns a pair of objects where the first is the lesser and the second is the greater. In the case of an initializer_list, the pair is the least object and the greatest object in the list.

Remarks

The first template function returns pair<const Type&, const Type&>(_Right, _Left) if _Right is less than _Left. Otherwise, it returns pair<const Type&, const Type&>(_Left, _Right).

The second member function returns a pair where the first element is the lesser and the second is the greater when compared by the predicate _Comp.

The remaining template functions behave the same, except that they replace the _Left and _Right parameters with _IList.

The function performs exactly one comparison.

minmax_element

template<class ForwardIterator>
    pair< ForwardIterator, ForwardIterator >
        minmax_element(
            ForwardIterator _First, 
            ForwardIterator _Last
 );
template<class ForwardIterator, class BinaryPredicate>
    pair< ForwardIterator, ForwardIterator >
        minmax_element(
            ForwardIterator _First, 
            ForwardIterator _Last, 
            BinaryPredicate _Comp
);

 

Parameters

_First

A forward iterator that indicates the beginning of a range.

_Last

A forward iterator that indicates the end of a range.

_Comp

An optional test used to order elements.

Return Value

pair<ForwardIterator, ForwardIterator>

( min_element (_First, _Last), max_element(_First, _Last)).

Remarks

The first template function returns

pair<ForwardIterator,ForwardIterator>

(min_element(_First, _Last),max_element(_First, _Last)).

The second template function behaves the same, except that it replaces operator<(X, Y) with _Comp(X, Y).

If the sequence is non-empty, the function performs at most 3 * (_Last - _First - 1) / 2 comparisons.

© 著作权归作者所有

NiuYoohoo
粉丝 1
博文 49
码字总数 18987
作品 0
赣州
私信 提问
如何將std::string轉成大寫(小寫)? (C/C++) (STL) (C)

Abstract C++的Standard Library並沒有提供將std::string轉成大寫和小寫的功能,只有在提供將char轉成大寫(toupper)和小寫(tolower)的功能而已,在此利用STL的transform配合toupper/tolower...

长平狐
2013/01/11
121
0
Google开源了Abseil,为C++和Python提供支持

Google公开了其项目内部使用的一系列C++库,随后还会公开其Python库。 Abseil已在Google历经十多年的开发,它的目的是为Google编程人员在各种项目上的工作需求提供支持,这些项目包括Protoco...

linuxprobe16
2017/10/27
0
0
STL list链表的用法详解

------------------------------------------------------------------------------- 原来... STL list链表的用法详解 本文以List容器为例子,介绍了STL的基本内容,从容器到迭代器,再到普通...

nao
2014/04/10
0
0
STL vector 介绍连载1-2-3

STL简介: STL = Standard Template Library,标准模板库,惠普实验室开发的一系列软件的统称。它是由Alexander Stepanov、Meng Lee和David R Musser在惠普实验室工作时所开发出来的。这可能是...

天远
2012/05/20
0
0
C++ STL学习——algorithm

在之前的博客中我们学习了很多STL中的模板库,包括deque,queue,stack,list等,他们都是一种数据结构,也就是说STL已经为我们实现了。今天我们来讲讲STL中比较大的一个库. 主要是一些算法的...

chenyufeng1991
2016/09/24
0
0

没有更多内容

加载失败,请刷新页面

加载更多

告诉大家写简历和面试的技巧(面向高级开发和架构师)

之前我也写过不少介绍面试相关的博文,有些文章的点击量还不低,这些面试相关的经验大多都是从我面试别人的过程中提炼出来了。 在18年底到19年3月,陆续面试了几家公司,有成有不成的,最终进...

java菜分享
36分钟前
6
0
SpringMVC教程3【文件上传下载,静态资源处理及数据校验】

一,文件上传 web.xml配置通用 <?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi......

薛小二
43分钟前
7
0
【跟着我们学Golang】基础结构

鉴于上篇文章我们已经讲过Go语言环境的安装,现在我们已经有了一个可以运行Go程序的环境,而且,我们还运行了'Hello World'跑出了我们的第一个Go程序。 这节我们就以'Hello World为例,讲解G...

搜云库技术团队
47分钟前
3
0
谈谈ConcurrentHashMap1.7和1.8的不同实现

ConcurrentHashMap 在多线程环境下,使用HashMap进行put操作时存在丢失数据的情况,为了避免这种bug的隐患,强烈建议使用ConcurrentHashMap代替HashMap,为了对更深入的了解,本文将对JDK1....

群星纪元
48分钟前
2
0
JBDC更新计数行以及调用存储过程返回多个结果集

在学习该知识点之前,需要了解sqlserver的一个存储过程语句:SET NOCOUNT ON; 执行该命令 表示不返回计数行,什么是计数行了,比如我们执行 DELETE ,UPDATE,INSERT的时候,对多少条数据进行了...

小v来上课
50分钟前
2
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部