std::ranges::partial_sort
来自 cppreference.cn
定义于头文件 <algorithm> |
||
调用签名 (Call signature) |
||
template< std::random_access_iterator I, std::sentinel_for<I> S, class Comp = ranges::less, class Proj = std::identity > |
(1) | (C++20 起) |
template< ranges::random_access_range R, class Comp = ranges::less, class Proj = std::identity > |
(2) | (C++20 起) |
1) 重新排列元素,使得范围
[
first,
middle)
包含范围 [
first,
last)
中最小的 middle - first 个已排序元素。 相等元素的顺序**不**保证保留。范围
[
middle,
last)
中剩余元素的顺序**未指定**。 元素使用给定的二元比较函数 comp 进行比较,并使用 proj 函数对象进行投影。
本页描述的类函数实体是 算法函数对象(非正式地称为 niebloids),即
目录 |
[编辑] 参数
first, last | - | 定义要重新排列元素的范围的迭代器-哨兵对 |
r | - | 要重新排列的元素范围 |
middle | - | 范围 [ first, middle) 将被排序 |
comp | - | 应用于投影元素的比较器 |
proj | - | 应用于元素的投影 |
[编辑] 返回值
一个等于 last 的迭代器。
[编辑] 复杂度
𝓞(N·log(M)) 次比较,两倍的投影操作,其中 N 是 ranges::distance(first, last),M 是 ranges::distance(first, middle)。
[编辑] 可能的实现
struct partial_sort_fn { template<std::random_access_iterator I, std::sentinel_for<I> S, class Comp = ranges::less, class Proj = std::identity> requires std::sortable<I, Comp, Proj> constexpr I operator()(I first, I middle, S last, Comp comp = {}, Proj proj = {}) const { if (first == middle) return ranges::next(first, last); ranges::make_heap(first, middle, comp, proj); auto it {middle}; for (; it != last; ++it) { if (std::invoke(comp, std::invoke(proj, *it), std::invoke(proj, *first))) { ranges::pop_heap(first, middle, comp, proj); ranges::iter_swap(middle - 1, it); ranges::push_heap(first, middle, comp, proj); } } ranges::sort_heap(first, middle, comp, proj); return it; } template<ranges::random_access_range R, class Comp = ranges::less, class Proj = std::identity> requires std::sortable<ranges::iterator_t<R>, Comp, Proj> constexpr ranges::borrowed_iterator_t<R> operator()(R&& r, ranges::iterator_t<R> middle, Comp comp = {}, Proj proj = {}) const { return (*this)(ranges::begin(r), std::move(middle), ranges::end(r), std::move(comp), std::move(proj)); } }; inline constexpr partial_sort_fn partial_sort {}; |
[编辑] 示例
运行此代码
#include <algorithm> #include <functional> #include <iostream> #include <string> #include <vector> void print(const auto& v) { for (const char e : v) std::cout << e << ' '; std::cout << '\n'; } void underscore(int n) { while (n-- > 0) std::cout << "^ "; std::cout << '\n'; } int main() { static_assert('A' < 'a'); std::vector<char> v {'x', 'P', 'y', 'C', 'z', 'w', 'P', 'o'}; print(v); const int m {3}; std::ranges::partial_sort(v, v.begin() + m); print(v), underscore(m); static_assert('1' < 'a'); std::string s {"3a1b41c5"}; print(s); std::ranges::partial_sort(s.begin(), s.begin() + m, s.end(), std::greater {}); print(s), underscore(m); }
输出
x P y C z w P o C P P y z x w o ^ ^ ^ 3 a 1 b 4 1 c 5 c b a 1 3 1 4 5 ^ ^ ^
[编辑] 参阅
(C++20) |
复制并部分排序一个范围的元素 (算法函数对象) |
(C++20) |
将一个范围按升序排序 (算法函数对象) |
(C++20) |
对一个范围的元素进行排序,同时保留相等元素之间的顺序 (算法函数对象) |
(C++20) |
部分排序给定的范围,确保它被给定的元素划分 (算法函数对象) |
(C++20) |
从一个元素范围创建一个最大堆 (算法函数对象) |
(C++20) |
从一个最大堆中移除最大的元素 (算法函数对象) |
(C++20) |
向一个最大堆添加一个元素 (算法函数对象) |
(C++20) |
将一个最大堆转换成一个按升序排序的元素范围 (算法函数对象) |
对一个范围的前 N 个元素进行排序 (函数模板) |