C++ Primer 学习中。。。

简单记录下我的学习过程 (代码为主)

min_element、max_element  找最小、最大值。 非常easy没什么大作用



#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; /*******************************************************************************************
template <class ForwardIterator>
ForwardIterator min_element ( ForwardIterator first, ForwardIterator last ); template <class ForwardIterator, class Compare>
ForwardIterator min_element ( ForwardIterator first, ForwardIterator last,
Compare comp ); eg:
template <class ForwardIterator>
ForwardIterator min_element ( ForwardIterator first, ForwardIterator last )
{
ForwardIterator lowest = first;
if (first==last) return last;
while (++first!=last)
if (*first<*lowest) // or: if (comp(*first,*lowest)) for the comp version
lowest=first;
return lowest;
}
********************************************************************************************/ /********************************************************************************************
template <class ForwardIterator>
ForwardIterator max_element ( ForwardIterator first, ForwardIterator last ); template <class ForwardIterator, class Compare>
ForwardIterator max_element ( ForwardIterator first, ForwardIterator last,
Compare comp );
eg:
template <class ForwardIterator>
ForwardIterator max_element ( ForwardIterator first, ForwardIterator last )
{
ForwardIterator largest = first;
if (first==last) return last;
while (++first!=last)
if (*largest<*first) // or: if (comp(*largest,*first)) for the comp version
largest=first;
return largest;
}
********************************************************************************************/ bool myfn(int i, int j)
{
return i<j;
} struct myclass
{
bool operator() (int i,int j)
{
return i<j;
}
} myobj; int main ()
{
int myints[] = {3,7,2,5,6,4,9}; // using default comparison:
cout << "The smallest element is " << *min_element(myints,myints+7) << endl;
cout << "The largest element is " << *max_element(myints,myints+7) << endl; // using function myfn as comp: 函数
cout << "The smallest element is " << *min_element(myints,myints+7,myfn) << endl;
cout << "The largest element is " << *max_element(myints,myints+7,myfn) << endl; // using object myobj as comp: 函数对象
cout << "The smallest element is " << *min_element(myints,myints+7,myobj) << endl;
cout << "The largest element is " << *max_element(myints,myints+7,myobj) << endl; return 0;
} /*********
Output:
The smallest element is 2
The largest element is 9
The smallest element is 2
The largest element is 9
The smallest element is 2
The largest element is 9
*********/

STL_算法_最小值和最大值(min_element、max_element)的更多相关文章

  1. cb27a_c++_STL_算法_最小值和最大值

    cb27a_c++_STL_算法_最小值和最大值min_element(b,e) b--begin(), e--end()min_element(b,e,op). op:函数,函数对象,一元谓词.ma ...

  2. STL_算法_中使用的函数对象

    写在前面: STL算法中的 函数对象的功能: (1).都是提供一种比较的函数,比较相邻的左右两个值的 相等/大小 等的关系, (2).返回值都是bool :该返回值 貌似是指明 遍历元素是否还要继续往 ...

  3. STL_算法_查找算法(lower_bound、upper_bound、equal_range)

    C++ Primer 学习中. .. 简单记录下我的学习过程 (代码为主) //全部容器适用(O(log(n)))    已序区间查找算法 lower_bound()        //找第一个符合的 ...

  4. STL_算法_查找算法(find、find_if)

    C++ Primer 学习中. .. 简单记录下我的学习过程 (代码为主) find . find_if /**********************线性查找O(n) find(); find_if ...

  5. STL_算法_逆转(reverse,reverse_copy)

    C++ Primer 学习中.. . 简单记录下我的学习过程 (代码为主) //全部容器适用 reverse(b,e)        //逆转区间数据 reverse_copy(b,e,b2) /** ...

  6. STL_算法_依据第n个元素排序(nth_element)

    C++ Primer 学习中... 简单记录下我的学习过程 (代码为主) //全部容器适用 nth_element(b,n,e) nth_element(b,n,e,p) 对照:partition() ...

  7. STL_算法_查找算法(binary_search、includes)

    C++ Primer 学习中.. . 简单记录下我的学习过程 (代码为主) 全部容器适用(O(log(n)))     已序区间查找算法 binary_search             //二分查 ...

  8. STL_算法_局部排序(partial_sort、partial_sort_copy)

    C++ Primer 学习中. . . 简单记录下我的学习过程 (代码为主) /***************************************** // partial_sort(b, ...

  9. STL_算法_区间的比較(equal、mismatch、 lexicographical_compare)

    C++ Primer 学习中.. . 简单记录下我的学习过程 (代码为主) //全部容器适用 equal(b,e,b2)       //用来比較第一个容器[b,e)和第二个容器b2开头,是否相等 e ...

随机推荐

  1. 条款24:若所有参数皆需要类型转换,请为此采用non-member函数(Declare non-member functions when type conversions should apply to all parameters)

    NOTE: 1.如果你需要为某个函数的所有参数(包括this指针所指的那个隐喻参数)进行类型转换,那么这个函数必须是个non-member.

  2. Django关于SQL注意事项

    执行原生SQL: from django.db import connection, connections cursor = connection.cursor() cursor.execute( ...

  3. POJ 3259 Wormholes(负权环路)

    题意: 农夫约翰农场里发现了很多虫洞,他是个超级冒险迷,想利用虫洞回到过去,看再回来的时候能不能看到没有离开之前的自己,农场里有N块地,M条路连接着两块地,W个虫洞,连接两块地的路是双向的,而虫洞是单 ...

  4. hdu 6201 transaction (最短路变形——带负权最长路)

    题意: 给定n个城市的货物买卖价格, 然后给定n-1条道路,每条路有不同的路费, 求出从某两个城市买卖一次的最大利润. 利润 = 卖价 - (买价 + 路费) 样例数据, 最近是从第一个点买入, 第4 ...

  5. Android GradientDrawable的XML实现

     Android GradientDrawable的XML实现 Android GradientDrawable与附录文章1类似,这次以XML而非Java代码形式实现.比如写好一个shape文件放 ...

  6. 【Ajax 1】Ajax与传统Web开发的区别

    导读:从用户体验度的角度来说,利用Ajax进行开发的网站,其体验度高于利用传统Web开发技术,那么,是什么因素导致了这一现象呢?难道说Ajax开发,就一定优于传统Web技术吗?本篇文章,将主要介绍Aj ...

  7. HDU2069-Coin Change

    Coin Change 这题题意和UVA674很像,但加了一个限制条件不能超过100个硬币.于是我们可以用d[i][j]来表示硬币数量为i时金钱总数为j时的方法总数,总钱不能超过250. const ...

  8. [HDU4348]To the moon(主席树)

    传送门 对于这个题,显然要打lazy标记了,但是lazy标记pushdown的时候肯定会增加一大堆节点,然后就MLE了.(题解这么说的,我其实不会pushdown) 所以,就换另一种方式,把标记直接打 ...

  9. 【git】git分支的合并

    原文: http://gitbook.liuhui998.com/3_3.html http://gitbook.liuhui998.com/5_3.html 一.如何分支的合并 在git中,可以使用 ...

  10. Visual Studio 2017 RC的坑

    ASP.NET Core Project add Docker Project Support的问题 执行上面操作以后,如果本机没有装好docker,就会一直报错,无法build通过,无论你在Proj ...