greater()和less()的使用
greater和less是头文件<xfunctional>中定义的两个结构。下面看它们 的定义,greater和less都重载了操作符()。
// TEMPLATE STRUCT greater
emplate<class _Ty>
struct greater
: public binary_function<_Ty, _Ty, bool>
{ // functor for operator>
bool operator()(const _Ty& _Left, const _Ty& _Right) const
{ // apply operator> to operands
return (_Left > _Right);
}
};
// TEMPLATE STRUCT less
emplate<class _Ty>
struct less
: public binary_function<_Ty, _Ty, bool>
{ // functor for operator<
bool operator()(const _Ty& _Left, const _Ty& _Right) const
{ // apply operator< to operands
return (_Left < _Right);
}
};
在sort()函数中使用greater<int>()和less<int>(),
#include<iostream>
#include<algorithm>//因为用了sort()函数
#include<functional>//因为用了greater<int>()
using namespace std;
void main()
{
int a[]={3,1,4,2,5};
int i;
int len=sizeof(a)/sizeof(int);//这里切记要除以sizeof(int)!
sort(a ,a + len, greater<int>());//内置类型的由大到小排序
for(i=0;i<len;i++)
cout<<a[i]<<" ";
cout<<"\n";
sort(a, a + len, less<int>()); //内置类型的由小到大排序
for(i=0;i<len;i++)
cout<<a[i]<<" ";
}
greater()和less()的使用的更多相关文章
- Transform a BST to greater sum tree
Given a BST, transform it into greater sum tree where each node contains sum of all nodes greater th ...
- R.java 文件内报错:Underscores can only be used with source level 1.7 or greater。
R.java 文件内报错:Underscores can only be used with source level 1.7 or greater 网上查找后得知是Android工程图片资源命名的问 ...
- Poj(2407),Greater New York Regional 2015 (D)
题目链接:http://poj.org/problem?id=2407 Relatives Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
- Binary search for the first element greater than target
We all know how to search through an array for an element whose value equals the target value, but h ...
- flume 报File Channel transaction capacity cannot be greater than the capacity of the channel capacity错误
今天在部署flume集群时,在启动collector服务器没报错,启动agent服务器报错: File Channel transaction capacity cannot be greater t ...
- 【转】System.Data.OracleClient requires Oracle client software version 8.1.7 or greater
安装完ASP.NET,Oracle9i客户端后,使用System.Data.OracleClient访问Oracle数据库如果出现这种错误:System.Data.OracleClient requi ...
- [leetcode-496-Next Greater Element I]
You are given two arrays (without duplicates) nums1 and nums2 where nums1's elements are subset of n ...
- [leetcode-556-Next Greater Element III]
Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exactly th ...
- Next Greater Element I
You are given two arrays (without duplicates) nums1 and nums2 where nums1's elements are subset of n ...
- LeetCode 538. Convert BST to Greater Tree (把二叉搜索树转换成较大树)
Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original B ...
随机推荐
- java的反射机制和javassist、asm
1.java的反射机制,可以帮助我们在运行的时候获取我们引用的java类相关的信息,包括类的名字.所包含的方法名字.方法参数等等 2.javassit这个jar包,大概看了下,更厉害,它可以直接操作字 ...
- 【网络】TCP的流量控制
一.利用滑动窗口实现流量控制 流量控制是让发送方的发生速率不要太快,要让接收方来得及接收. 发送方的发送窗口不能超过接收方给出的接收窗口的数值,TCP的窗口单位是字节,不是报文段. TCP为每一个连接 ...
- html 元素定位position-relative, absolute, fixed, static
看到这个,你有什么想法? Difference between static and relative positioning 如果你能完全看明白,那几本上css 元素定位的东西基本都会了.本文也不用 ...
- VS2015 android 设计器不能可视化问题解决。
近期安装了VS2015,体验了一下android 的开发,按模板创建执行了个,试下效果非常不错.也能够可视化设计.但昨天再次打开或创建一个android程序后,设计界面直接不能显示,显示错误:(可能是 ...
- B-Tree 索引和 Hash 索引的对照
对于 B-tree 和 hash 数据结构的理解可以有助于预測不同存储引擎下使用不同索引的查询性能的差异.尤其是那些同意你选择 B-tree 或者 hash 索引的内存存储引擎. B-Tree 索引的 ...
- Solidworks drwdot文件如何打开,如何制作Solidworks工程图模板
1 直接把这个文件拖放进Solidworks窗口 2 文件-保存图纸格式,另存为模板(slddrt文件) 3 搜索"Solidworks工程图如何使用,替换图纸格式模板文件.doc& ...
- 分享一个基于Bootstrap的 ACE框架 入门(MVC+EF)
基于Bootstrap3,拥有强大的功能组件以及UI组件,基本能满足后台管理系统的需求, 而且能根据不同设备适配显示,而且还有四个主题可以切换. 简单入门,源代码下载:https://github.c ...
- FancyCoverFlow
https://github.com/davidschreiber/FancyCoverFlow
- Android NDK编程浅入深出之--Android.mk
Android.mk Android.mk是一个向Android NDK构建系统描写叙述NDK项目的GUN Makefile片段.它是每个NDK项目的必备组件. 构建系统希望它出如今jni子文 ...
- BC1.2的一些心得
什么叫DCD DataContact Detect(DCD) 1.首先是DCD 2.然后是Primary detection 3.然后是Secondary detection 检測充电的条件是VBUS ...