定义<对于STL排序算法 - 操作符重载,函数或独立函数? [英] Defining < for STL sort algorithm - operator overload, functor or standalone function?

查看:136
本文介绍了定义<对于STL排序算法 - 操作符重载,函数或独立函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个stl :: list包含Widget类对象。它们需要根据Widget类中的两个成员进行排序。



为了排序工作,必须定义一个小于比较两个Widget对象的比较器。似乎有无数的方法来做到这一点。从我可以收集,可以:



a。在类中定义比较运算符重载:

  bool Widget :: operator< (const Widget& rhs)const 

定义一个具有两个Widget的独立函数:

  bool operator<(const Widget& lhs,const Widget& rhs); 

然后让Widget类成为它的朋友:

  class Widget {
//各种类定义...
friend bool operator<(const Widget& lhs,const Widget& rhs);
};

c。定义函数,然后在调用排序函数时将其作为参数包含:

  class Widget_Less:
public binary_function< Widget,Widget,bool> {
bool operator()(const Widget& lhs,const Widget& rhs)const;
};

有人知道哪种方法更好吗?特别是我有兴趣知道我是否应该做1或2.我搜索了有效的STL由斯科特·迈尔,但不幸的是没有任何关于这一点。



感谢您的回复。

解决方案

如果您只是将两个小部件彼此比较,请使用成员 operator 。如果你正在比较Widget和别的,定义一个全局运算符< (两个参数版本,可选的Widget类的一个朋友,但这是一个单独的问题。 p>

函子你真的只想要如果你做一些不太正统的东西。如果一个小于的比较在小部件的上下文中没有意义,选择一个函子。这种情况下,运算符< 可能会令人困惑。当然,函子仍然必须提供一个排序,但只是因为它是一个排序并不意味着它是一个小于操作(例如,对于函数排序状态可能比运算符<更好。


I have a stl::list containing Widget class objects. They need to be sorted according to two members in the Widget class.

For the sorting to work, a less-than comparator comparing two Widget objects must be defined. There seems to be a myriad of ways to do it. From what I can gather, one can either:

a. Define a comparison operator overload in the class:

bool Widget::operator< (const Widget &rhs) const

b. Define a standalone function taking two Widgets:

bool operator<(const Widget& lhs, const Widget& rhs);

And then make the Widget class a friend of it:

class Widget {
    // Various class definitions ...
    friend bool operator<(const Widget& lhs, const Widget& rhs);
};

c. Define a functor and then include it as a parameter when calling the sort function:

class Widget_Less :
public binary_function<Widget, Widget, bool> { 
    bool operator()(const Widget &lhs, const Widget& rhs) const;
};

Does anybody know which method is better? In particular I am interested to know if I should do 1 or 2. I searched the book Effective STL by Scott Meyer but unfortunately it does not have anything to say about this.

Thank you for your reply.

解决方案

If you are only comparing two Widgets to each other, use a member operator <. If you are comparing Widget to something else, define a global operator < (the two parameter version, optionally a friend of the Widget class but that is a separate issue.

Functor you really only want if you are doing something a little less orthodox. Choose a functor if a "less than" comparison doesn't make sense in the context of widgets. In that case, having operator < could be confusing. Of course, functors still have to provide an ordering, but just because it is an ordering doesn't really mean it is a "less than" operation. (Example, sorting states by population is probably better for a functor than an operator <.

这篇关于定义&lt;对于STL排序算法 - 操作符重载,函数或独立函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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