对std :: list进行排序 [英] sorting a std::list

查看:534
本文介绍了对std :: list进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试获取一个列表并将其传递给std :: sort,如下所示:

sort(Unsorted.begin(),Unsorted.end());

我收到一条错误,说明列表迭代器没有

二元减法运算符。

在algoritm头文件中偷看它很明显为什么看到那种类型

有调用_sort(_First,_Last,_Last-_First)这可能是一点点

挑战看到存储在列表中的不同值不是

需要连续存储。

我的问题是有另一种方法对STL中的列表进行排序吗?

I tried taking a list and pass it through std::sort like the following:
sort(Unsorted.begin(), Unsorted.end());
I got an error back stating that the list iterator doesn''t have a
binary substraction operator.
Peeking in the algoritm header file it''s clear why seeing that sort
there calls _sort(_First, _Last, _Last-_First) which might be a tad
challenging seeing that the different values stored in a list do not
need to be stored consecutively.
My question is there another way to sort the list in the STL?

推荐答案

ve*********@hotmail.com 写道:
我尝试了一个列表并通过std :: sort传递它,如下所示:
sort(Unsorted.begin(),Unsorted.end());
我得到一个错误,说明列表迭代器没有
二进制替换n操作员。
偷看algoritm头文件,很清楚为什么看到那种类型
会调用_sort(_First,_Last,_Last-_First)这可能有点挑战存储在列表中的不同值不需要连续存储。
我的问题是有另一种方法对STL中的列表进行排序吗?
I tried taking a list and pass it through std::sort like the following:
sort(Unsorted.begin(), Unsorted.end());
I got an error back stating that the list iterator doesn''t have a
binary substraction operator.
Peeking in the algoritm header file it''s clear why seeing that sort
there calls _sort(_First, _Last, _Last-_First) which might be a tad
challenging seeing that the different values stored in a list do not
need to be stored consecutively.
My question is there another way to sort the list in the STL?



I想你想要


list :: sort(CompFunc op)


其中


op( elem1,elem2)


比较两个元素?


(参见''C ++标准库'' - Josuttis p 245-6)


毕竟,你有一个东西的列表 - elem1'和elem2' -

a类型据推测,某些订购方式可以使用。

比较函数采用任意两个元素并确定它们的相对

排名。如果没有办法告诉,实施可能会抱怨。

希望这会有所帮助。


-


干杯

-

Hewson :: Mike

这封信比平时长,因为我没时间做这件事了/>
更短 - Blaise Pascal


I think you want

list::sort( CompFunc op )

where

op( elem1, elem2 )

compares two elements?

( See ''The C++ Standard Library" - Josuttis p 245-6 )

After all, you have a list of somethings - the elem1''s and elem2''s - of
a type for which some way of ordering is presumably available. The
comparison function takes any two elements and determines their relative
ranking. The implementation may well complain if it has no way of telling.
Hope this helps.

--

Cheers
--
Hewson::Mike
"This letter is longer than usual because I lack the time to make it
shorter" - Blaise Pascal


list :: sort(CompFunc op)
list::sort( CompFunc op )




你知道,我觉得现在真的很蠢。我不知道我怎么会错过

列表的排序功能。



You know, I feel really stupid now. I don''t know how I managed to miss
the sort function of list.


Mike Hewson写道:
Mike Hewson wrote:
我想你想要

list :: sort(CompFunc op)

其中

op(elem1,elem2)<比较两个元素?

(参见''C ++标准库' - Josuttis p 245-6)

毕竟,你有一个清单某些东西 - elem1'和elem2' -
这种类型的某种订购方式可能是可用的。
比较函数采用任意两个元素并确定其相对的排名。如果没有办法告诉,实施可能会抱怨。
希望这会有所帮助。
I think you want

list::sort( CompFunc op )

where

op( elem1, elem2 )

compares two elements?

( See ''The C++ Standard Library" - Josuttis p 245-6 )

After all, you have a list of somethings - the elem1''s and elem2''s -
of a type for which some way of ordering is presumably available. The
comparison function takes any two elements and determines their relative
ranking. The implementation may well complain if it has no way of telling.
Hope this helps.




我也发现(Josuttis再次发表第397页)std :: sort和

std :: stable_sort使用* random access iterators *。因此不能用于

列表,因为它们不适用于那种类型的容器。


-

干杯

-

Hewson :: Mike

这封信比往常长,因为我没时间让它更短b $ b更短 - Blaise Pascal



Also I found ( Josuttis again p 397 ) that std::sort and
std::stable_sort use *random access iterators*. Hence can''t be used for
lists, as they are not provided for that type of container.

--

Cheers
--
Hewson::Mike
"This letter is longer than usual because I lack the time to make it
shorter" - Blaise Pascal


这篇关于对std :: list进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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