简体   繁体   English

运算符使用自定义类重载C ++ STL映射

[英]Operator overloading of C++ STL map with custom classes

I have a doubt regarding using stl map in C++. 我对在C ++中使用stl map有疑问。 I know using map with custom classes I need to overload "<" operator to make map work. 我知道使用带有自定义类的map我需要重载“<”运算符以使map工作。 But how do I define it in a meaningful way. 但是我如何以有意义的方式定义它。 For example I have the following code 例如,我有以下代码

#include <iostream>
#include <map>
using namespace std;

struct box{
    int e,s,w;
    box(): e(-1), s(-2), w(-3)
    {}

       bool operator< (const box& lhs) const
       {
           return e < lhs.e;
       }

};

int main() {
    // your code goes here
    map<box, int> hashtable;
    box b;
    hashtable[b] = 1;
    return 0;
}

Here I have overloaded the < operator very trivially. 在这里,我非常琐碎地重载了<运算符。 I could have overloaded it as follows too 我也可以按如下方式重载它

bool operator< (const box& lhs) const
{
    return w+s+e < lhs.e+lhs.s+lhs.w;
}

And there are other ways too. 还有其他方法。 So my question is, does this, overloading of < operator, affect the search, delete time of accessing the elements in map. 所以我的问题是,这样做,<operator的重载会影响搜索,删除访问地图中元素的时间。 I mean does it affect hashing part of the maps. 我的意思是它是否影响地图的散列部分。 If so, what is the best way to overload < operator. 如果是这样,重载<运算符的最佳方法是什么。

My only motive here is to store pairs of box and int (see in the main function), so that I can access them in O(log(n)) time. 我唯一的动机是存储box和int对(参见main函数),这样我就可以在O(log(n))时间访问它们。

UPDATE UPDATE

I figured that having a shitty comparator does not affect the access, delete time of maps but rather have a impact on the keys present in the map. 我认为有一个糟糕的比较器不会影响访问,删除地图的时间,而是影响地图中的键。 For example if my comparator were the following 例如,如果我的比较器如下

bool operator< (const box& lhs) const
{
    return e < lhs.e;
}

and now lets say I two tuples (e,s,w) as (1,2,3) and (1,3,4). 现在让我说两个元组(e,s,w)为(1,2,3)和(1,3,4)。 I want to insert it into the above map. 我想将它插入上面的地图。 Now because I have comparator which solely determines on the basis of value of "e", it will reject the second tuple. 现在因为我有比较器,它只根据“e”的值来确定,它将拒绝第二个元组。 So finally the map will contains the (1,2,3) not the other tuples. 所以最后地图将包含(1,2,3)而不是其他元组。

The best way to write a comparator is using std::tie , as suggested by @edgar in the accepted answer. 编写比较器的最佳方法是使用std :: tie ,正如@edgar在接受的答案中所建议的那样。

bool operator< (const box& lhs) const
{
    return std::tie(e,w,s) < std::tie(lhs.e,lhs.w,lhs.s);
}

In this two tuples are different even if the tuples have different ordering. 在这两个元组中,即使元组具有不同的顺序,它们也是不同的。 I had a requirement of that in my question. 在我的问题中我有这个要求。 For example (1,2,3) is different than (2,1,3). 例如(1,2,3)与(2,1,3)不同。 Had I use the following comparator 我是否使用以下比较器

bool operator< (const box& lhs) const
{
    return e+w+s < lhs.e+lhs.w+lhs.s;
}

Again only the first tuple would have made it because both these tuples have same sum, so again not a good comparator. 再次只有第一个元组会成功,因为这两个元组都有相同的总和,所以再次不是一个好的比较器。

So my question is, does this, overloading of < operator, affect the search, delete time of accessing the elements in map. 所以我的问题是,这样做,<operator的重载会影响搜索,删除访问地图中元素的时间。

No, it is guaranteed that search, deletion and accessing elements are performed in logarithmic time. 不,保证在对数时间内执行搜索,删除和访问元素。

I mean does it affect hashing part of the maps. 我的意思是它是否影响地图的散列部分。

std::map is not std::unordered_map , so there is no hashing here. std :: map不是std :: unordered_map ,所以这里没有散列。

If so, what is the best way to overload < operator. 如果是这样,重载<运算符的最佳方法是什么。

I suppose that the standard way now is to use std::tie : 我想现在的标准方法是使用std :: tie

bool operator<(const box& lhs) const
{
    return std::tie(e, s, w) < std::tie(lhs.e, lhs.s, lhs.w);
}

std::map is not a hash map, it's a binary tree. std::map不是哈希映射,它是二叉树。

operator< is invoked every time the tree is accessed, once for each lookup step. 每次访问树时都会调用operator< ,每次查找一次都会调用一次。 So obviously the complexity of it affects performance (overhead aside, the cost of eg a lookup in an std::map is proportional to the cost of operator< ). 显然,它的复杂性会影响性能(除了开销之外,例如std::map查找的成本与operator<的成本成正比)。

std::unordered_map is a hash map. std::unordered_map是一个哈希映射。 But for that you need to implement std::hash<box> and std::equal_to<box> (hashing and equality functors). 但是为此你需要实现std::hash<box>std::equal_to<box> (散列和等式函子)。 operator< isn't used in that case. 在这种情况下不使用operator<

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

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