#include <unordered_set>
#include <iostream>
class edge{
public:
float a1;
float a2;
};
struct comp{
bool operator()(const edge& e1, const edge& e2) const {
return true;
return (
(e1.a1==e2.a1 && e1.a2==e2.a2) ||
(e1.a1==e2.a2 && e1.a2==e2.a1)
);
};
};
struct hash{
size_t operator()(const edge& e1) const {
// return std::hash<float>()(e1.a1+e1.a2);
return std::hash<float>()(e1.a1+e1.a2*2);
};
};
int main() {
std::unordered_set<edge,hash,comp> s1;
s1.insert(edge{1.1,2.2});
s1.insert(edge{2.2,1.1});
for( auto& it : s1 ) {
std::cout << it.a1 << " " << it.a2 << "\n";
}
std::cout << "s1.size " << s1.size() << "\n";
}
I realize that if different element has same hash value, then they are considered equal, but I just want this unordered_set use comparator that I define, just ignore hash?
How to achieve that?
I know i can use set, but using set need to consider order, if a < b is true and b < a is also true, then this element will not be inserted successfully, Sometimes, It is hard to provide order.
If anyone can help, much appreciated
edited:
My intention is to let two edges called e1,e2, they are same if (e1.a1==e2.a1&&e1.a2==e2.a2)
or(e1.a1==e2.a2 && e1.a2==e2.a1)
as I provided
in struct comp.
but when i test. it seems hash function can change the comparison too. Someone says the way I define hash and comparator result in undefined behaviour.
Is that true? why?
if true, how to solve this? I just want comparator decide which one is satisfied to be inserted in unordered_set without duplicate. And really do not care about hash.
BTW, thanks for some many people replying