STL和排序列表 [英] STL and sorted list

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

问题描述

我需要实现一个排序(有序)列表。据我所知,STL没有排序列表类型为

。是否有(直接)方式使用

STL实现排序列表?

BTW:列表中的项目类型将是一个类。是否有必要实施>
或者<运算符或写一个比较函数,它返回两个类中较大或较小的两个?b $ b?理想情况下,列表以

最小元素开头。


干杯,

Matthias

解决方案

Der Andere写道:

我需要实现一个排序(有序)列表。据我所知,STL没有排序列表
类型。


它有std :: set。

是否有(直接)方式使用STL实现排序列表?
BTW:类型列表中的项目将是一个类。是否有必要实施>或者<运算符或写一个比较函数,它可以返回两个类中较大或较小的类?


只有运算符<,AFAIK。

理想情况下,列表以最小元素开头。




在文章< c9 ************* @ news.t-online.com>,

" Der Andere" <毫安************** @ gmx.net>写道:

我需要实现一个排序(有序)列表。据我所知,STL没有排序列表类型。是否有(直接)方式使用
STL实现排序列表?


当然。


myList.sort();


BTW:物品类型在列表中将是一个类。是否有必要实施>或者<运算符或写一个比较函数返回两个类中较大或较小的一个?




operator<通常用于排序,但任何二元仿函数

都可以比较两个对象,并返回较小的(即最左边的)

一个将起作用。


> >我需要实现一个排序(有序)列表。 STL没有排序列表类型

as

据我所知。是否有(直接)方法使用
STL实现排序列表?



当然。

myList.sort();




由于列表必须在每次(非排序)插入后进行排序,

可能多重集是更好的选择。

BTW:列表中的项目类型将是一个类。是否有必要实施>或者<运算符或写一个比较函数,
返回两个类中较大或较小的一个?



运算符<通常用于排序,但任何二元仿函数可以比较两个对象并返回较小的(即最左边的)
一个将工作。



I need to implement a sorted (ordered) list. STL has no sorted list type as
far as I know. Is there a (straight) way to implement a sorted list using
STL?
BTW: The type of items in the list will be a class. Is it necessary to
implement the > or < operators or to write a compare-function that returns
the larger or smaller of two classes? Ideally, the list begins with the
smallest element.

Cheers,
Matthias

解决方案

Der Andere wrote:

I need to implement a sorted (ordered) list. STL has no sorted list
type as far as I know.
It has std::set.
Is there a (straight) way to implement a sorted list using STL?
BTW: The type of items in the list will be a class. Is it necessary to
implement the > or < operators or to write a compare-function that
returns the larger or smaller of two classes?
Only operator<, AFAIK.
Ideally, the list begins with the smallest element.




In article <c9*************@news.t-online.com>,
"Der Andere" <ma**************@gmx.net> wrote:

I need to implement a sorted (ordered) list. STL has no sorted list type as
far as I know. Is there a (straight) way to implement a sorted list using
STL?
Sure.

myList.sort();

BTW: The type of items in the list will be a class. Is it necessary to
implement the > or < operators or to write a compare-function that returns
the larger or smaller of two classes?



operator< is generally implemented for sort, but any binary functor that
can compare two objects and return true for the smaller (ie left most)
one will work.


> >I need to implement a sorted (ordered) list. STL has no sorted list type
as

far as I know. Is there a (straight) way to implement a sorted list using
STL?



Sure.

myList.sort();



As the list would have to be sorted after every (non-sorting) insertion,
probably the multiset is the better alternative.

BTW: The type of items in the list will be a class. Is it necessary to
implement the > or < operators or to write a compare-function that returnsthe larger or smaller of two classes?



operator< is generally implemented for sort, but any binary functor that
can compare two objects and return true for the smaller (ie left most)
one will work.



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

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