Created
July 26, 2013 16:12
-
-
Save vbo/6090142 to your computer and use it in GitHub Desktop.
C++11 unordered_set of custom data type. See http://stackoverflow.com/questions/17885563/using-c11-unordered-set-in-visual-c-and-clang/17885727?noredirect=1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Clang build cmdline: | |
// $ clang++ ./set.cpp -Wall -Werror -Wfatal-errors -std=c++11 -stdlib=libc++ -o set.out | |
// | |
#include <iostream> | |
#include <unordered_set> | |
struct Point { | |
int x, y; | |
Point(int x = 0, int y = 0) { | |
this->x = x; | |
this->y = y; | |
} | |
bool operator==(Point const& p) const { | |
return this->x == p.x && this->y == p.y; | |
} | |
}; | |
namespace std { | |
template<> struct hash<Point> { | |
size_t operator()(const Point &pt) const { | |
return std::hash<int>()(pt.x) ^ std::hash<int>()(pt.y); | |
} | |
}; | |
} | |
typedef std::unordered_set<Point> points_set_t; | |
int main() { | |
Point point1(1, 5); | |
Point point2(1, 1); | |
Point point3(1, 5); | |
points_set_t points; | |
points.insert(point1); | |
points.insert(point2); | |
points.insert(point3); | |
for (points_set_t::const_iterator it = points.begin(); it != points.end(); it++) { | |
std::cout << it->x << ":" << it->y << std::endl; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment