提问人:unknown 提问时间:12/4/2022 更新时间:12/25/2022 访问量:186
C++ 是否有一个内置partial_sort,可以返回排序值的位置
c++ is there a built in partial_sort that returns the location of the sorted values
问:
我有一个 N 个元素的列表,想要找到最小(或最大)M 值的位置。 是否有内置函数(沿着 std::sort 或 std::p artial_sort 的思路)可以做到这一点?
答:
1赞
Michaël Roy
12/25/2022
#1
没有内置函数。但是您可以尝试如下操作:
- 在原始数组中创建迭代器向量。这将比 pair<index、value>s 的向量占用更少的位置,并且可以让您以最快的速度访问原始数据,这是 nth_element() 尽可能高效地运行所必需的。
- 在向量上调用 std::nth_element()。
- 通过调用 std::d istance(或减法,对于 c++98)来获取索引。
如:
template <typename Fn>
std::vector<size_t> GetMElementsPositions(const std::vector<int>& v, size_t m,
Fn&& compare) {
assert(m != 0);
assert(m <= v.size());
std::vector<std::vector<int>::const_iterator> w;
w.reserve(v.size());
for (auto i = v.begin(); i != v.end(); ++i)
w.push_back(i);
std::nth_element(w.begin(), w.begin() + M - 1, w.end(), [&compare](auto& x, auto& y) { return compare(*x, *y); });
std::vector<size_t> r;
r.reserve(M);
for (auto i = w.begin(); i != w.begin() + M; ++i)
r.push_back(std::distance(v.begin(), *i));
return r;
}
您还可以跳过 std::d istance()à 部分,并将结果裁剪为大小(如果 M 比原始数据集大小小得多,则复制到较小的数组中。使用迭代器就像使用指针一样简单,而且比使用索引更有效。
你可以在这里找到一个工作原型:https://godbolt.org/z/YjjoanoTb
评论
N
std::make_heap
M
N
std::nth_element
std::nth_element
m
nth_element