【C++ 】sort利用函数排序
https://leetcode-cn.com/problems/ba-shu-zu-pai-cheng-zui-xiao-de-shu-lcof/
补记:应注意:
sort(first,last,cmp):
中的cmp在编写自定义比较的时候,相等情况应当返回false,不然会出bug。
https://en.cppreference.com/w/cpp/named_req/Compare中写道:
严格是说在判断的时候会用"<",而不是"<="
1、sort的简单应用:
sort - C++ Reference (cplusplus.com)
// sort algorithm example #include <iostream> // std::cout #include <algorithm> // std::sort #include <vector> // std::vector bool myfunction(int i, int j) { return (i < j); } struct myclass { bool operator() (int i, int j) { return (i < j); } } myobject; int main() { int myints[] = { 32,71,12,45,26,80,53,33 }; std::vector<int> myvector(myints, myints + 8); // 32 71 12 45 26 80 53 33 // using default comparison (operator <): std::sort(myvector.begin(), myvector.begin() + 4); //(12 32 45 71)26 80 53 33 // using function as comp std::sort(myvector.begin() + 4, myvector.end(), myfunction); // 12 32 45 71(26 33 53 80) // using object as comp std::sort(myvector.begin(), myvector.end(), myobject); //(12 26 32 33 45 53 71 80) // print out content: std::cout << "myvector contains:"; for (std::vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; }
2、cmp函数的理解
返回值为true,vector等容器按照调用顺序进行排列,否则反序。
这里面比较难理解的是利用myobject和myfunction进行建立排序规则。理解是:myobject、myfunction中的i,j都是待排序的对象,其形参则是
sort(x.begin(), x.end(),....);
中"x"的一个元素。
即对于sort(first,last,cmp):
first, lastRandom-access iterators to the initial and final positions of the sequence to be sorted. The range used is [first,last)
, which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
shall point to a type for which
swap is properly defined and which is both move-constructible and move-assignable.
compBinary function that accepts two elements in the range as arguments, and returns a value convertible to bool
. The value returned indicates whether the element passed as first argument is considered to go before the second in the specific strict weak ordering it defines.
The function shall not modify any of its arguments.
This can either be a function pointer or a function object.
如果对于一个二维vector的每一行二元数组的第二个元素进行排序,代码是这么写的:
435. 无重叠区间 - 力扣(LeetCode) (leetcode-cn.com)
class Solution { public: // 按照区间右边界排序 static bool cmp (const vector<int>& a, const vector<int>& b) { return a[1] < b[1]; } int eraseOverlapIntervals(vector<vector<int>>& intervals) { if (intervals.size() == 0) return 0; sort(intervals.begin(), intervals.end(), cmp); int count = 1; // 记录非交叉区间的个数 int end = intervals[0][1]; for (int i = 1; i < intervals.size(); i++) { if (end <= intervals[i][0]) { end = intervals[i][1]; count++; } } return intervals.size() - count; } };
由于这里对应的"x"是二维数组,待排序的对象元素是a[1],而cmp的形参是“x”的元素,也就是一个vector<int>。
sort(first,last,cmp):
严格是说在判断的时候会用"<",而不是"<="
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异