读书人

sort()算法,该如何处理

发布时间: 2012-08-17 02:08:34 作者: rapoo

sort()算法
sort()算法为什么需要随机访问
挨个比较不行吗?

[解决办法]
这取决于用哪种sort算法,而STL的sort用了快速排序,所以需要随机访问。
[解决办法]
找中间值时用到
[解决办法]
sort
template<class RanIt>
void sort(RanIt first, RanIt last);
template<class RanIt, class Pred>
void sort(RanIt first, RanIt last, Pred pr);
The first template function reorders the sequence designated by iterators in the range [first, last) to form a sequence ordered by operator<. Thus, the elements are sorted in ascending order.

The function evaluates the ordering predicate X < Y at most ceil((last - first) * log(last - first)) times.

The second template function behaves the same, except that it replaces operator<(X, Y) with pr(X, Y).

读书人网 >C++

热点推荐