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 ...
随机推荐
- Docker+Drone做Java/Tomcat的CI服务
1. 安装Docker(略过) 2. 编写docker-compose.yaml version: '2' services: drone-server: image: drone/drone:0.8 ...
- [Bash] Find Files and Folders with `find` in Bash
find is a powerful tool that can not only find files but it can run a command on each matching file ...
- 让你的 EditText 所有清除
原文地址:让你的 EditText 所有清除 參考原文:Giving your Edit Texts the All Clear 项目地址(欢迎 Star):ClearEditText 在输入文本的时 ...
- hdu 5087 Revenge of LIS II ( LIS ,第二长子序列)
链接:hdu 5087 题意:求第二大的最长升序子序列 分析:这里的第二大指的是,全部的递增子序列的长度(包含相等的), 从大到小排序后.排在第二的长度 cid=546" style=&qu ...
- InnoDB: Error: io_setup() failed with EAGAIN after 5 attempts
在一台server中以各数据库的备份文件为数据文件启动多个MySQL实例供SQL Review使用. 之前执行一直没有问题(最多的时候有23个MySQL实例同一时候执行).后来新配置了一台server ...
- 《coredump问题原理探究》Linux x86版7.9节list相关的iterator对象
这一节.看一下list的iterator对象在内存的布局 1 #include <list> 2 3 void init( std::list<int>& lst ) ...
- Eclipse导入项目: No projects are found to import
Eclipse导入项目: No projects are found to import 如果发导入工程import的时候,出现”No projects are found to import” 的 ...
- IIS+Asp.Net Mvc必须知道的事(解决启动/重启/自动回收站点后第一次访问慢问题)
问题现象: Asp.net Mvc站点部署在IIS上后,第一个用户第一次访问站点,都会比较慢,确切的说是访问站点的Action页面(即非静态页面,因为静态页面直接由IIS处理返回给用户即完成请求,而A ...
- IO获取指定目录及其目录下子目录
一.需求:获取指定目录下,指定扩展名的文件(包含子目录中的) 二.需要用到的方法 1.FilenameFilter :返回抽象路径名的定义中表示此抽象路径名的目录中的文件的数组. filter.ac ...
- CocoaPoda在iOS开发中的使用
CocoaPoda在iOS开发中的使用 CocoaPods 简介 CocoaPods是iOS开发中不可避免的依赖管理第三方的工具,能简化一些第三方库文件需要添加编译参数及依赖库的繁复工作 CocoaP ...