自定义排序需要单独写一个compare函数

例1 LeetCode 056. Merge Intervals

Given a collection of intervals, merge all overlapping intervals.

For example,
Given [1,3],[2,6],[8,10],[15,18],
return [1,6],[8,10],[15,18].

 /**
* Definition for an interval.
* struct Interval {
* int start;
* int end;
* Interval() : start(0), end(0) {}
* Interval(int s, int e) : start(s), end(e) {}
* };
*/
class Solution {
public:
vector<Interval> merge(vector<Interval>& ins) {
if (ins.empty())
return vector<Interval>{};
vector<Interval> res;
sort(ins.begin(), ins.end(), [](Interval a, Interval b){return a.start < b.start;});
res.push_back(ins[]); for (int i = ; i < ins.size(); i++) {
if (res.back().end < ins[i].start)
res.push_back(ins[i]);
else
res.back().end = max(res.back().end, ins[i].end);
}
return res;
}
};

  函数写法:

 /**
* Definition for an interval.
* struct Interval {
* int start;
* int end;
* Interval() : start(0), end(0) {}
* Interval(int s, int e) : start(s), end(e) {}
* };
*/
bool mySort(const Interval &a, const Interval &b) {
return a.start < b.start;
}
class Solution {
public:
vector<Interval> merge(vector<Interval>& ins) {
if (ins.empty())
return vector<Interval>{};
vector<Interval> res;
sort(ins.begin(), ins.end(), mySort);
res.push_back(ins[]); for (int i = ; i < ins.size(); i++) {
if (res.back().end < ins[i].start)
res.push_back(ins[i]);
else
res.back().end = max(res.back().end, ins[i].end);
}
return res;
}
};

  注意到compare函数写在类外,这是因为

  std::sort要求函数对象,或是静态/全局函数指针,非静态成员函数指针不能直接传递给std::sort
 
 

【C++】标准库sort函数的自定义排序的更多相关文章

  1. 使用STL库sort函数对vector进行排序

    使用STL库sort函数对vector进行排序,vector的内容为对象的指针,而不是对象. 代码如下 #include <stdio.h> #include <vector> ...

  2. c/c++ 标准库 bind 函数 详解

    标准库 bind 函数 详解 bind函数:接收一个函数名作为参数,生成一个新的函数. auto newCallable = bind(callbale, arg_list); arg_list中的参 ...

  3. Atitit 数据库 标准库  sdk 函数库 编程语言 mysql oracle  attilax总结

    Atitit 数据库 标准库  sdk 函数库 编程语言 mysql oracle  attilax总结 1.1. 常见的编程语言以及数据库 sql内部函数库标准化库一般有以下api1 1.2. 各个 ...

  4. 标准库中 vector list等排序

    1.list自带有排序函数sort():可以定义自己的排序规则,如: struct stTest { int count; wstring str; }; bool SortByNum(const s ...

  5. 定制对ArrayList的sort方法的自定义排序

    java中的ArrayList需要通过collections类的sort方法来进行排序 如果想自定义排序方式则需要有类来实现Comparator接口并重写compare方法 调用sort方法时将Arr ...

  6. ptyhon 编程基础之函数篇(二)-----返回函数,自定义排序函数,闭包,匿名函数

    一.自定义排序函数 在Python中可以使用内置函数sorted(list)进行排序: 结果如下图所示: 但sorted也是一个高阶函数,可以接受两个参数来实现自定义排序函数,第一个参数为要排序的集合 ...

  7. c++多线程编程:实现标准库accumulate函数的并行计算版本

    今天使用c++实现了标准库头文件<numeric>中的accumulate函数的并行计算版本,代码如下,注释写的比较详细,仅对其中几点进行描述: ①该实现假定不发生任何异常,故没有对可能产 ...

  8. sort函数使用自定义数据排序使用

    package main import ( "fmt" "sort" ) type ServerSlice []Server type Server struc ...

  9. C标准库常用函数概要

    stdio.h printf()/fprintf() printf的返回值是打印的字符数, 发生错误则返回负数 scanf()/fscanf() scanf的返回值是成功赋值的变量个数, 失败则返回E ...

随机推荐

  1. 配置过滤器filter对跨站脚本攻击XSS实现拦截

    1.web.xml中配置filter [html] view plain copy   <filter> <filter-name></filter-name> & ...

  2. Python——Numpy的random子库

    NumPy的random子库 np.random.* np.random.rand() np.random.randn() np.random.randint() import numpy as np ...

  3. 淘宝开放平台php-sdk测试 获取淘宝商品信息(转)

    今天想使用淘宝开放平台的API获取商品详情,可是以前一直没使用过,看起来有点高深莫测,后然看开发入门,一步一步,还真有点感觉了,然后看示例,还真行了,记下来以后参考.其中遇到问题,后然解决了.因为我已 ...

  4. SQL语法结构

    目录 一.增 1.增加字段 2.新建约束 二.删 1.删除字段 2.删除约束 三.改 1.修改字段 一.增 1.增加字段: ALTER TABLE [表名] ADD [字段名] NVARCHAR () ...

  5. java搭建 SpringMVC+Mybatis(SMM)+mybatis-generate

    搭建SSM系统,首先要了解整个过程: 1.创建spring-mvc项目 2.在maven中添加要引用的jar包(使用框架都是较新的版本:) 3. jdbc.xml +spring-mybatis.xm ...

  6. Data Structure Linked List: Write a function to get the intersection point of two Linked Lists.

    http://www.geeksforgeeks.org/write-a-function-to-get-the-intersection-point-of-two-linked-lists/ 第一第 ...

  7. mini2440移植uboot 2014.04(三)

    我修改的代码已经上传到github上,地址:https://github.com/qiaoyuguo/u-boot-2014.04-mini2440.git 参考文档: s3c2440手册(下载地址) ...

  8. 如何阻止form表单中的button按钮提交

    <form action="#" method="post"> <input type="text" name=" ...

  9. FOJ 2213 简单几何

    题意:给你两个圆的圆心坐标和半径,判断两个圆公切线数目. 思路:考虑两个圆间公切线的情况,两个圆的位置关系分为相离,相交,外切,内切,内含,重合,公切线数分别为4,2,3,1,0,-1. #inclu ...

  10. 最小生成树prim算法 POJ2031

    #include<iostream> #include<algorithm> #include<string.h> #include<ctype.h> ...