相关疑难解决方法(0)

使用自定义类类型作为键的C++ unordered_map

我正在尝试使用自定义类作为关键字unordered_map,如下所示:

#include <iostream>
#include <algorithm>
#include <unordered_map>

using namespace std;

class node;
class Solution;

class Node {
public:
    int a;
    int b; 
    int c;
    Node(){}
    Node(vector<int> v) {
        sort(v.begin(), v.end());
        a = v[0];       
        b = v[1];       
        c = v[2];       
    }

    bool operator==(Node i) {
        if ( i.a==this->a && i.b==this->b &&i.c==this->c ) {
            return true;
        } else {
            return false;
        }
    }
};

int main() {
    unordered_map<Node, int> m;    

    vector<int> v;
    v.push_back(3);
    v.push_back(8);
    v.push_back(9);
    Node n(v);

    m[n] = 0; …
Run Code Online (Sandbox Code Playgroud)

c++ hash unordered-map g++ hashtree

258
推荐指数
3
解决办法
22万
查看次数

标签 统计

c++ ×1

g++ ×1

hash ×1

hashtree ×1

unordered-map ×1