C ++ operator overloading with STL card with user classes

I have doubts about using a stl card in C ++. I know, using a map with custom classes, I need to overload the operator "<" to make the map work. But how can 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 overloaded the <operator is very trivial. I could overload it also as follows

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

And there are other ways. So my question is whether this overloads the <operator, affects the search, removes the access time to the elements on the map. I mean, does this affect the hashing of part of the cards. If so, what is the best way to overload the <statement.

box int (. ), O (log (n)) .

UPDATE

, , , , . ,

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

, , (e, s, w) (1,2,3) (1,3,4). . , , "e", . , (1,2,3), .

:: , @edgar .

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

, . . , (1,2,3) (2,1,3).

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

, , .

+4
2

, , < , , .

, , , .

, .

std:: map std:: unordered_map, .

, < .

, std:: tie:

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

std::map -, .

operator< , . , , ( , , , std::map, operator<).

std::unordered_map -. std::hash<box> std::equal_to<box> ( ). operator< .

+1

Source: https://habr.com/ru/post/1668119/


All Articles