gpt4 book ai didi

c++ - 什么时候对标量调用 stable_sort()?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:28:47 25 4
gpt4 key购买 nike

在标量类型(即 intlong、等)与默认比较器?

如果是这样,你应该什么时候做?

如果不是,那么为什么标准库不直接将此类调用转发给 sort?那不是更快吗?

最佳答案

稳定排序仅在您排序的项目具有 satellite information 时才有用。 .


摘自 CLRS(算法导论,第 3 版):

"In practice, the numbers to be sorted are rarely isolated values. Each is usually part of a collection of data called a record. Each record contains a key, which is the value to be sorted. The remainder of the record consists of satellite data, which are usually carried around with the key. In practice, when a sorting algorithm permutes the keys, it must permute the satellite data as well."


当排序稳定时,这意味着已排序数组中的关系被项目的原始顺序打破。如果您只对 intlong 类型进行排序,则不需要稳定排序。

关于c++ - 什么时候对标量调用 stable_sort()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17256810/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com