0
#include <iostream>
#include <unordered_set>
#include <set>
using namespace std;

struct Cube {
    int x;
    int y;
    int number;
    bool canRemove;
    Cube(int x, int y, int number) : x(x), y(y), number(number) {}
    bool operator == (const Cube &c) const {
        return true;
    }
};

int main () { 
    int m;
    cin >> m;
    unordered_set<Cube> s;
    //unordered_set<Cube> s1;
    //unordered_set<Cube> s2;
    for (int i = 0; i < m; i++) {
        int x, y;
        cin >> x >> y;
        s.insert(Cube(x, y, i));
    }
    cout << s.size() << endl;
    return 0;
}

For my understanding: I should overload < for set, because it is a binary search tree. I should overload == for unordered_set, because it is a hash table. Please correct me if I am wrong.

Question:

1 the code can't compile, please help me fix it.

2 for this line:

bool operator < (const Cube &c) const {

if I delete the second const. It also can't compile for set why I need the second const? why I can't overload < this way?

bool operator < ( Cube &c) {
}
Shawn
  • 27
  • 1
  • 6
  • Compiler error for 1) ? – deviantfan Mar 31 '15 at 07:03
  • It's all in the name. "unordered" doesn't suggest BST. You're thinking of `std::set`. – juanchopanza Mar 31 '15 at 07:03
  • unordered_set use hash not use operator – Ron Tang Mar 31 '15 at 07:04
  • Because the duplicate doesn´t answer the second question (or I haven´t seen it yet): If the implementation wants to apply the function on const variables, it can´t work without your const. Apparently it does use it on const variables. (And it makes no sense that a comparison like < and > changes the value) – deviantfan Mar 31 '15 at 07:08

0 Answers0