简体   繁体   English

当我使用STL集时,我应该重载==运算符吗?

[英]should I overload == operator when I use STL sets?

当我想使用STL集来存储C ++中的自定义对象时,有很多人说:你应该重载<operator,但如果我想使用find()方法,我认为它可能会使用==运算符来实现它。任何人都可以给我一些指示?

The Standard Library set::find uses equivalence instead of equality to find values. 标准库 set::find使用等价而不是相等来查找值。 You don't need to provide operator == , just operator < (or whatever comparison operator you specified for set , std::less being the default). 您不需要提供operator == ,只需要operator < (或者您为set指定的任何比较运算符, std::less是默认值)。

If you are wondering how your element would be found, then assuming the default ordering find(x) would return the element e for which: 如果您想知道如何找到您的元素,那么假设默认排序find(x)将返回元素e ,其中:

!( x < e || e < x )

Since std::set s require something that specifies a strict weak ordering . 因为std::set需要指定严格弱序的东西。 operator== is insufficient for this task. operator==不足以完成此任务。

You should overload operator< only if it makes sense for the class. 只有在对类有意义时才应重载operator< If it doesn't, far better would be utilizing the fact that std::set has as a second template parameter Compare . 如果没有,那么更好的是利用std::set作为第二个模板参数Compare的事实。 Hence, defining a comparison struct/function and passing this as the 2nd parameter of your set is another, generally preferable option. 因此,定义比较结构/函数并将其作为集合的第二个参数传递是另一个通常更可取的选项。

The final option is to specialize std::less for your type. 最后一个选项是为你的类型专门化std::less For example: 例如:

namespace std
{ 
    template <>
    struct less<CustomClass>
    { ... };
}

Most Standard Library algorithms and containers use operator< (or a comparison function you provide such that it returns true if the lhs element is smaller) for ordering and searching. 对于排序和搜索,大多数标准库算法和容器使用operator <(或您提供的比较函数,使其在lhs元素较小时返回true)。 Algorithms for use with unordered containers will use operator==. 与无序容器一起使用的算法将使用operator ==。

For example, std::lower_bound() will either return the first element matching the search criteria from a sorted container, the largest element still smaller than the search term if the exact term is not found, or an iterator to the end of the container if no element is larger than what you searched for. 例如,std :: lower_bound()将从已排序的容器返回与搜索条件匹配的第一个元素,如果未找到确切的术语,则最大元素仍然小于搜索项,或者是容器末尾的迭代器如果没有元素大于您搜索的元素。 It does this with operator<, no other operator is required. 它通过operator <执行此操作,不需要其他操作员。 It takes a number of operations in the order of log(n). 它按log(n)的顺序执行许多操作。

Every other comparison operator (>, >=, <=), with the exception of != and ==, can be derived from operator<. 每个其他比较运算符(>,> =,<=),除了!=和==,都可以从operator <派生。

std::find, however, requires that the type used be Equality Comparable. 但是,std :: find要求使用的类型为Equality Comparable。 You can find the reference here: http://en.cppreference.com/w/cpp/algorithm/find 你可以在这里找到参考资料: http//en.cppreference.com/w/cpp/algorithm/find

So if you're working with ordered elements, you need operator<. 因此,如果您正在使用有序元素,则需要operator <。 If you are working with unordered elements, you need operator==. 如果您正在使用无序元素,则需要operator ==。

set , map , multiset and multimap use only the comparison function you give them, which defaults to std::less , which is generally operator< . setmapmultisetmultimap只使用你给它们的比较函数,默认为std::less ,通常是operator< The unordered version in C++11 have a more complex protocol; C ++ 11中的无序版本具有更复杂的协议; they don't need an order comparison (they're unordered ) but they need both equality and hash. 他们不需要订单比较(他们是无序的 ),但他们需要相等和哈希。

This is also true of all of the standard algorithms which involve ordering ( sort , nth_element , lower_bound , binary_search , etc.). 所有涉及排序的标准算法( sortnth_elementlower_boundbinary_search等)也是如此。 However, the find algorithm (and other similar algorithms, such as count , search , mismatch , etc., do require an equality function, defaulting to operator== . No standard library algorithm requires both. 然而, find 算法 (和其他类似的算法,如countsearchmismatch等)确实需要一个相等的函数,默认为operator== 。没有标准库算法需要两者。

Since you specifically asked about the find member function, the answer would be that the set and all member functions will work fine with just order comparison. 由于您特别询问了find成员函数,因此答案是set和所有成员函数只需进行顺序比较即可正常工作。

Still, it's almost always a good idea to define all comparison operators if you're going to define operator< . 但是,如果你要定义operator<那么定义所有比较运算符几乎总是一个好主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM