Guest User

Untitled

a guest
Nov 19th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.79 KB | None | 0 0
  1. #include<iostream>
  2. #include<unordered_map>
  3.  
  4. using namespace std;
  5. class Line {
  6. public:
  7. float m;
  8. float c;
  9.  
  10. Line() {m = 0; c = 0;}
  11. Line(float mInput, float cInput) {m = mInput; c = cInput;}
  12. float getM() const {return m;}
  13. float getC() const {return c;}
  14. void setM(float mInput) {m = mInput;}
  15. void setC(float cInput) {c = cInput;}
  16.  
  17. bool operator==(const Line &anotherLine) const
  18. {
  19. return (m == anotherLine.m);
  20. }
  21. };
  22.  
  23. namespace std
  24. {
  25. template <>
  26. struct hash<Line>
  27. {
  28. size_t operator()(const Line& k) const
  29. {
  30. // Compute individual hash values for two data members and combine them using XOR and bit shifting
  31. return ((hash<float>()(k.getM()) ^ (hash<float>()(k.getC()) << 1)) >> 1);
  32. }
  33. };
  34. }
  35.  
  36. int main()
  37. {
  38. unordered_map<Line, int> t;
  39.  
  40. Line line1 = Line(3.0,4.0);
  41. Line line2 = Line(3.0,5.0);
  42.  
  43. t.insert({line1, 1});
  44. auto x = t.insert({line2, 2});
  45. if (x.second == false)
  46. cout << "insert failed" << endl;
  47.  
  48. for(unordered_map<Line, int>::const_iterator it = t.begin(); it != t.end(); it++)
  49. {
  50. Line t = it->first;
  51. cout << t.m << " " << t.c << "n" ;
  52. }
  53.  
  54. return 1;
  55. }
Add Comment
Please, Sign In to add comment