自定义排序需要单独写一个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. python 写一个类似于top的监控脚本

    最近老板给提出一个需要,项目需求大致如下:      1.用树莓派作为网关,底层接多个ZigBee传感节点,网关把ZigBee传感节点采集到的信息通过串口接收汇总,并且发送给上层的HTTP Serve ...

  2. C - Common Subsequence

    C - Common Subsequence Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I ...

  3. Linux环境安装Nginx详细步骤

    1.yum解决编译nginx所需的依赖包,之后你的nginx就不会报错了yum install gcc patch libffi-devel python-devel  zlib-devel bzip ...

  4. HDU 4824 Disk Schedule

    //

  5. Python——Numpy的random子库

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

  6. Redis持久化——RDB(一)

    核心知识点: 1.RDB:将当前数据生成快照保存到硬盘 2.手动触发 save:会阻塞Redis服务器直到RDB完成. bgsave:执行fork创建子进程,由子进程负责RDB操作,阻塞只发生在for ...

  7. 改善程序与设计的55个具体做法 day2

    条款05:了解C++默默编写并调用哪些函数 如果没有为类定义构造函数.析构函数.拷贝构造函数.重载赋值操作符,并且这些函数被需要(调用)时,编译器会为类生成默认的函数,而这些函数是public inl ...

  8. PAT 天梯赛 L2-010. 排座位 【并查集】

    题目链接 https://www.patest.cn/contests/gplt/L2-010 思路 因为 题意中 朋友的朋友 就是朋友 那么 朋友的关系 用 并查集 保存 但是 敌对关系 只有直接的 ...

  9. 用Java实现断点续传的基本思路和代码

    用Java实现断点续传的基本思路和代码   URL url = new URL(http://www.oschina.net/no-exist.zip); HttpURLConnection http ...

  10. Data Structure Binary Tree: Construct Full Binary Tree from given preorder and postorder traversals

    http://www.geeksforgeeks.org/full-and-complete-binary-tree-from-given-preorder-and-postorder-travers ...