As an example of using std::unordered_set consider:
Code:#include <iostream> #include <unordered_set> struct Pos { int i {}; int j {}; Pos(int i_, int j_) : i(i_), j(j_) {} }; struct Hash { size_t operator()(const Pos& p) const { return p.i * 100 + p.j; } }; struct Equal { bool operator()(const Pos& e1, const Pos& e2) const { return e1.i == e2.i && e1.j == e2.j; } }; using Used = std::unordered_set<Pos, Hash, Equal>; bool insert(Used& used, int x, int y) { if (!used.emplace(x, y).second) { std::cout << '(' << x << ", " << y << ") already exists\n"; return false; } return true; } int main() { Used used; insert(used, 1, 2); insert(used, 2, 3); insert(used, 1, 2); }




Reply With Quote
