以结构体为键值的map

C++中有时可能会需要使用以结构体(struct)为键值的(map)

like this:

struct Node
{
    LL x, y;
    friend bool operator<(const Node& a, const Node& b) {
        if (a.x < b.x || (a.x == b.x && a.y < b.y)) {
            return true;
        }
        return false;
    }
}p[N];

map<Node, LL> mp;

但是重载运算符中加不加(friend)影响巨大。

只有加上(friend)才能够通过编译。

我的猜想是重载(<)的时候如果不加(friend),则该函数没法访问到(map)的变量(应该是(private)(protected)的所以没法直接访问),就会报错。加了(friend)的话可以访问(map)类的所有(private)(protected)的成员。

原文地址:https://www.cnblogs.com/Nepenthe8/p/13954128.html