“stable_sort()ing” STL< list>在C ++中 [英] "stable_sort()ing" a STL <list> in C++

查看:209
本文介绍了“stable_sort()ing” STL< list>在C ++中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为问题标题很清楚:是可能的stable_sort()在C ++的std ::列表?或者,我必须将其转换为std :: vector?

I think the question title is clear enough: is is possible to stable_sort() a std::list in C++? Or do I have to convert it to a std::vector?

我问的是因为我尝试了一个简单的例子,似乎需要RandomAccessIterators,没有。因此,如何稳定排序std :: list()

I'm asking because I tried a simple example and it seems to require RandomAccessIterators, which a linked list doesn't have. So, how do I stable sort a std::list()?

EDIT:我有一个错误:

#include <list>
#include <algorithm>
// ...
list<int> the_list;
stable_sort(the_list.begin(), the_list.end());

g ++给我大约30行错误(太长的粘贴),其中一些指RandomAccessIterators(和名为_merge_sort_loop的东西)。这是有点奇怪,因为我已经看到一些链接列表的合并排序实现,它们是非常连续的。

g++ gives me around 30 lines of errors (too long to paste), with some of them referring to RandomAccessIterators (and something called _merge_sort_loop). It's a little weird, since I've seen some merge sort implementations for linked lists and they are pretty much 'sequential'.

推荐答案

std :: list :: sort已经稳定了。从标准中,第23.2.24节:注意:稳定:保持等效元素的相对顺序。

std::list::sort is already stable. From the standard, section 23.2.24: "Notes: Stable: the relative order of the equivalent elements is preserved."

这篇关于“stable_sort()ing” STL&lt; list&gt;在C ++中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆