Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<unordered_map>
- using namespace std;
- class Line {
- public:
- float m;
- float c;
- Line() {m = 0; c = 0;}
- Line(float mInput, float cInput) {m = mInput; c = cInput;}
- float getM() const {return m;}
- float getC() const {return c;}
- void setM(float mInput) {m = mInput;}
- void setC(float cInput) {c = cInput;}
- bool operator==(const Line &anotherLine) const
- {
- return (m == anotherLine.m);
- }
- };
- namespace std
- {
- template <>
- struct hash<Line>
- {
- size_t operator()(const Line& k) const
- {
- // Compute individual hash values for two data members and combine them using XOR and bit shifting
- return ((hash<float>()(k.getM()) ^ (hash<float>()(k.getC()) << 1)) >> 1);
- }
- };
- }
- int main()
- {
- unordered_map<Line, int> t;
- Line line1 = Line(3.0,4.0);
- Line line2 = Line(3.0,5.0);
- t.insert({line1, 1});
- auto x = t.insert({line2, 2});
- if (x.second == false)
- cout << "insert failed" << endl;
- for(unordered_map<Line, int>::const_iterator it = t.begin(); it != t.end(); it++)
- {
- Line t = it->first;
- cout << t.m << " " << t.c << "n" ;
- }
- return 1;
- }
Add Comment
Please, Sign In to add comment