关于STL的排序,和比较参数cmp

本文介绍C++标准库中的stable_sort函数,该函数对指定范围内的元素进行排序,并保持相同元素的相对顺序不变。文中提供了使用默认比较及自定义比较函数的例子。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

stable_sort

template <class RandomAccessIterator> void stable_sort ( RandomAccessIterator first, RandomAccessIterator last );

  void stable_sort ( RandomAccessIterator first, RandomAccessIterator last,
                     Compare comp );

Sort elements preserving order of equivalents

Sorts the elements in the range  [first,last) into ascending order, like  sort, but  stable_sort grants that the relative order of the elements with equivalent values is preserved.

The elements are compared using  operator< for the first version, and  comp for the second.

Parameters

first, last
  [first,last)
comp
Comparison function object that, taking two values of the same type than those contained in the range, returns  true  if the first argument goes before the second argument in the specific strict weak ordering(严格偏序) it defines, and false otherwise.

bool cmp(Record a,Record b);
默认是增序,即若a.key < b.key, return true,按增序排列。
若a.key > b.key return true,则实际是按减序排列的
也可以定义相等时的大小比较来定义第二阶的排序

 
1046. Plane Spotting
 

 

### 如何在 `sort` 函数中使用 `cmp` 参数实现自定义排序 在 C++ 中,标准库中的 `std::sort` 可以接受一个可选的第三个参数作为比较器(即 `cmp`),用于指定自定义的排序逻辑。这个比较器是一个二元谓词,返回布尔值,决定两个元素之间的顺序。 以下是具体实现方法: #### 自定义比较函数 可以定义一个普通的函数来充当比较器: ```cpp bool compare(int a, int b) { return a < b; // 升序排列 } ``` 调用 `std::sort` 时传递该函数名即可: ```cpp #include <algorithm> #include <vector> int main() { std::vector<int> vec = {5, 3, 8, 6}; std::sort(vec.begin(), vec.end(), compare); return 0; } ``` 如果希望降序排列,则修改比较器逻辑为 `return a > b;`[^1]。 --- #### 使用 Lambda 表达式 Lambda 表达式提供了一种更简洁的方式定义比较器: ```cpp #include <algorithm> #include <vector> int main() { std::vector<int> vec = {5, 3, 8, 6}; std::sort(vec.begin(), vec.end(), [](const int& a, const int& b) -> bool { return a > b; // 降序排列 }); return 0; } ``` 上述代码通过匿名函数实现了降序排序[^2]。 --- ### 优先队列(`priority_queue`)的自定义排序方式 对于 `std::priority_queue`,可以通过模板参数指定自定义比较器。默认情况下,`std::priority_queue` 是最大堆(按降序排列)。要改变这种行为,需传入自定义比较器。 #### 定义结构体并重载 `<` 运算符 假设有一个名为 `Node` 的结构体,可以通过重载小于运算符 (`<`) 来实现自定义排序: ```cpp struct Node { int value; bool operator<(const Node& other) const { return value > other.value; // 小顶堆 } }; // 创建小顶堆 std::priority_queue<Node, std::vector<Node>, std::greater<Node>> pq; ``` 此处利用了 STL 提供的标准比较器 `std::greater<T>` 实现升序的小顶堆。 --- #### 使用函数对象或 Lambda 表达式 也可以直接定义一个函数对象或者使用 Lambda 表达式完成相同功能: ```cpp struct CompareNodes { bool operator()(const Node& lhs, const Node& rhs) const { return lhs.value > rhs.value; // 小顶堆 } }; // 使用自定义比较器创建优先队列 std::priority_queue<Node, std::vector<Node>, CompareNodes> pq; ``` 或者采用 Lambda 表达式的写法: ```cpp auto cmp = [](const Node& lhs, const Node& rhs) -> bool { return lhs.value > rhs.value; // 小顶堆 }; std::priority_queue<Node, std::vector<Node>, decltype(cmp)> pq(cmp); ``` 这种方式更加灵活,适合复杂场景下的定制化需求。 --- ### 总结 无论是 `std::sort` 还是 `std::priority_queue`,都可以通过自定义比较器实现特定的排序规则。前者支持任意范围内的数组或容器排序,而后者则适用于动态维护有序集合的需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值