使用 STL 容器进行中值计算时,正确的方法是什么?

2022-01-07 00:00:00 algorithm containers c++ stl median

假设我需要从 1000000 个随机数值序列中检索中位数.

Let's say I need to retrieve the median from a sequence of 1000000 random numeric values.

如果使用任何但 std::list,我没有(内置)方法来对序列进行排序以进行中位数计算.

If using anything but std::list, I have no (built-in) way to sort sequence for median calculation.

如果使用 std::list,我无法随机访问值来检索排序序列的中间(中值).

If using std::list, I can't randomly access values to retrieve middle (median) of sorted sequence.

自己实现排序并使用例如更好吗?std::vector,还是使用 std::list 和使用 std::list::iterator for-loop-步行到中值?后者看起来不那么开销,但也感觉更难看..

Is it better to implement sorting myself and go with e.g. std::vector, or is it better to use std::list and use std::list::iterator to for-loop-walk to the median value? The latter seems less overheadish, but also feels more ugly..

或者对我来说有更多更好的选择吗?

Or are there more and better alternatives for me?

推荐答案

任何随机访问容器(如 std::vector)都可以使用标准的 std::sort 算法,在 标头中可用.

Any random-access container (like std::vector) can be sorted with the standard std::sort algorithm, available in the <algorithm> header.

为了求中位数,使用std::nth_element会更快;这足以将一个选定的元素放在正确的位置,但并没有完全对容器进行排序.所以你可以找到这样的中位数:

For finding the median, it would be quicker to use std::nth_element; this does enough of a sort to put one chosen element in the correct position, but doesn't completely sort the container. So you could find the median like this:

int median(vector<int> &v)
{
    size_t n = v.size() / 2;
    nth_element(v.begin(), v.begin()+n, v.end());
    return v[n];
}

相关文章