sort函数怎么实现从大到小排序 sort(a.begin(), a.end()); // 对两组整数排序 sort(b.begin(), b.end());将这两个sort函数修改成从大到小排序
时间: 2024-02-29 12:53:10 浏览: 37
要实现从大到小排序,只需要在sort函数中传入一个降序的比较函数即可。可以使用lambda表达式来实现:
```
sort(a.begin(), a.end(), [](int x, int y) { return x > y; }); // 从大到小排序
sort(b.begin(), b.end(), [](int x, int y) { return x > y; });
```
这样就可以将两个sort函数修改成从大到小排序了。
相关问题
sort(A.begin(), A.end()); // 快速排序
快速排序是一种常用的排序算法,其时间复杂度为 O(nlogn)。它的基本思想是通过一趟排序将待排记录分割成独立的两部分,其中一部分记录的关键字均比另一部分记录的关键字小,然后再按此方法对这两部分记录分别进行快速排序,以达到整个序列有序的目的。
在 C++ 中,可以使用 `sort()` 函数实现快速排序。该函数的用法为:
```cpp
#include <algorithm>
// ...
sort(A.begin(), A.end());
```
其中,`A` 是待排序的数组,`begin()` 和 `end()` 分别是数组的起始位置和终止位置。注意,这里要包含头文件 `<algorithm>`。
用opencvsharp编写函数实现以下功能 // 极值点评分 void Caliper::ExtremePointRating() { if(extremePoints.empty()) return; // 评分: value * (1 - |i - 0.5 * le| / 0.5 * len) int halfLen = static_cast<int>(path.size() / 2); for(auto& e: extremePoints) { e.score = e.value * (1 - static_cast<double>(std::abs(e.ps - halfLen)) / halfLen); } //按评分排序:降序 sort(extremePoints.begin(), extremePoints.end(), CompareExtremPointInfoBysocre); result.peak = extremePoints.at(0).pt; if(extremePoints.size() == 1) { result.center = result.front = result.back = extremePoints.at(0).pt; return; } if(extremePoints.size() == 2) { int a = std::abs(extremePoints.at(0).ps - halfLen); int b = std::abs(extremePoints.at(1).ps - halfLen); if(a > b) { result.front = result.back = extremePoints.at(0).pt; result.center = extremePoints.at(1).pt; }else { } }else { std::vector<ExtremPointInfo> epf(extremePoints.begin(), extremePoints.begin() + 3); sort(epf.begin(), epf.end(), CompareExtremPointInfoByPs);//降序 result.back = epf.at(0).pt; result.center = epf.at(1).pt; result.front = epf.at(2).pt; } }
以下是使用 OpenCvSharp3 编写的实现上述功能的代码:
```csharp
using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Linq;
public class Caliper
{
public void ExtremePointRating()
{
if (extremePoints.Count == 0)
return;
int halfLen = path.Count / 2;
foreach (var e in extremePoints)
{
e.score = e.value * (1 - Math.Abs(e.ps - 0.5 * path.Count) / halfLen);
}
extremePoints.Sort(new CompareExtremPointInfoBysocre());
result.peak = extremePoints[0].pt;
if (extremePoints.Count == 1)
{
result.center = result.front = result.back = extremePoints[0].pt;
return;
}
if (extremePoints.Count == 2)
{
int a = Math.Abs(extremePoints[0].ps - halfLen);
int b = Math.Abs(extremePoints[1].ps - halfLen);
if (a > b)
{
result.front = result.back = extremePoints[0].pt;
result.center = extremePoints[1].pt;
}
else
{
result.front = result.back = extremePoints[1].pt;
result.center = extremePoints[0].pt;
}
return;
}
var epf = extremePoints.Take(3).ToList();
epf.Sort(new CompareExtremPointInfoByPs());
result.back = epf[0].pt;
result.center = epf[1].pt;
result.front = epf[2].pt;
}
private class CompareExtremPointInfoBysocre : IComparer<ExtremPointInfo>
{
public int Compare(ExtremPointInfo x, ExtremPointInfo y)
{
return -x.score.CompareTo(y.score);
}
}
private class CompareExtremPointInfoByPs : IComparer<ExtremPointInfo>
{
public int Compare(ExtremPointInfo x, ExtremPointInfo y)
{
return -x.ps.CompareTo(y.ps);
}
}
}
```
注意,由于缺少完整的代码,我无法保证此代码的完全正确性。因此,如果您在使用此代码时遇到任何问题,请自行进行调试和修改。
阅读全文