STL中list的sort 疑惑
STL中list的sort源码为:
template <class T, class Alloc>
void list<T, Alloc>::sort() {
if (node->next == node || link_type(node->next)->next == node) return;
list<T, Alloc> carry;
list<T, Alloc> counter[64];
int fill = 0;
while (!empty()) {
carry.splice(carry.begin(), *this, begin());
int i = 0;
while(i < fill && !counter[i].empty()) {
counter[i].merge(carry);
carry.swap(counter[i++]);
}
carry.swap(counter[i]);
if (i == fill) ++fill;
}
for (int i = 1; i < fill; ++i)
counter[i].merge(counter[i-1]);
swap(counter[fill-1]);
}
侯捷在书中说这是快排,但本人弄不清楚该算法,而且我总觉得 fill 有可能一直加到大于64.
望了解的人解释下。
template <class T, class Alloc>
void list<T, Alloc>::sort() {
if (node->next == node || link_type(node->next)->next == node) return;
list<T, Alloc> carry;
list<T, Alloc> counter[64];
int fill = 0;
while (!empty()) {
carry.splice(carry.begin(), *this, begin());
int i = 0;
while(i < fill && !counter[i].empty()) {
counter[i].merge(carry);
carry.swap(counter[i++]);
}
carry.swap(counter[i]);
if (i == fill) ++fill;
}
for (int i = 1; i < fill; ++i)
counter[i].merge(counter[i-1]);
swap(counter[fill-1]);
}
侯捷在书中说这是快排,但本人弄不清楚该算法,而且我总觉得 fill 有可能一直加到大于64.
望了解的人解释下。
...全文
相关推荐
详细解说 STL 排序(Sort)
### 详细解析 STL 排序(Sort):深入探索 C++ 的强大工具#### 前言:为何STL是你必须掌握的技能?对于任何程序员而言,数据结构与算法是编程旅程中的基石。
STL list链表用法详解
### STL List链表用法详解#### 一、引言随着C++的发展,标准模板库(Standard Template Library,简称STL)成为了该语言的重要组成部分。
C++语言 STL容器list总结
"这篇文章除了介绍C++ STL中的list容器的基本概念和特性外,还探讨了如何构造、操作和遍历list容器,包括插入、删除、排序、合并等高级功能。"C++标准模板库(STL)提供了一个名为`l
c++ STL之list对结构体的增加,删除,排序等操作详解
本文将深入探讨C++标准模板库(STL)中的list容器在处理结构体方面的操作,包括添加、删除、排序以及常见的遍历方法。作者Frank Yu于2018年7月14日编写了这段代码,旨在提供对list数据
STL list链表的用法详细解析
**排序list** 对于list,可以使用`sort()`成员函数对其进行排序,如`myList.sort();`。11.


