简体   繁体   English

为什么std :: sort和partial_sort需要随机访问迭代器?

[英]why do std::sort and partial_sort require random-access iterators?

I was wondering why does the c++ standard require that std::sort should only take random-access iterators? 我想知道为什么c ++标准要求std::sort应该只采用随机访问迭代器? I don't see the advantage, since both std::sort and std::list::sort have a complexity of N*log(N) . 我没有看到优势,因为std :: sortstd :: list :: sort都具有N*log(N)的复杂性。 Restricting std::sort to random-access iterators (RAI) seems to have made it necessary to write a separate function for lists with the same complexity. std::sort限制为随机访问迭代器(RAI)似乎已经使得必须为具有相同复杂性的列表编写单独的函数。

The same applies to partial_sort , where the non-RAI counter-part for list is simply missing to this day. 这同样适用于partial_sort ,其中列表的非RAI计数器部分至今仍然缺失

Is this design because people used variants of quick_sort to implement std::sort historically? 这个设计是因为人们使用quick_sort变体std::sort历史地实现std::sort吗?

If there is advantage for writing sort algorithms on RAI containers, would it better to make std::sort more general, and let RAI containers like std::vector provide specialized v.sort ? 如果在RAI容器上编写排序算法有好处,那么使std::sort更通用v.sort吗,让std::vector这样的RAI容器提供专门的v.sort吗?

O(N*log(N)) complexity doesn't imply that the container is iterated in order, nor that the changes to it are only made in scan order. O(N*log(N))复杂度并不意味着容器按顺序迭代,也不意味着对它的更改仅按扫描顺序进行。 To use sequential iterators would come at a O(N) memory cost to store all of those iterators. 使用顺序迭代器将以O(N)内存成本来存储所有这些迭代器。

Algorithm complexity doesn't say it all. 算法复杂性并不能说明一切。 Actually in C++ (from a formal point of view) it doesn't say anything because you cannot grow N to infinity ( size_t isn't an arbitrary precision number) thus every sort algorithm written in C++ is (formally) also O(1) . 实际上在C ++中(从正式的角度来看)它没有​​说什么,因为你不能将N增长到无穷大( size_t不是任意精度数)因此用C ++编写的每个排序算法(正式地)也是O(1)

From a practical point of view std::sort is the grandson of qsort and it's implemented most probably as a variation of quicksort. 从实际的角度来看, std::sortqsort的孙子,它最有可能实现为quicksort的变种。

Using merge sort for arrays would require an extra storage proportional to the size of the array (the link to next element) while merge sort for lists doesn't require any extra space because you can reuse the link already present in the node (that it's going to be destroyed by sorting anyway). 对数组使用合并排序需要一个与数组大小成比例的额外存储(指向下一个元素的链接),而列表的合并排序不需要任何额外空间,因为您可以重用节点中已存在的链接(它是无论如何都会被排序破坏)。

The idea of programming without knowing what kind of container you're dealing with is mostly an illusion, thus having two different explicit ways to sort efficiently two different types of containers is not considered bad per se. 在不知道你正在处理什么样的容器的情况下进行编程的想法主要是幻觉,因此有两种不同的显式方法来有效地对两种不同类型的容器进行排序本身并不被认为是错误的。

It's indeed annoying that std::sort doesn't contain a specialization also for list iterators (I'm not a template metaprogramming guru but it would seem quite straightforward to do). 确实令人讨厌的是std::sort也不包含列表迭代器的特化(我不是模板元编程大师,但它看起来很简单)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM