Advertisement
dreadmachine

hash_table.cpp

Dec 10th, 2021
498
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.20 KB | None | 0 0
  1. #include <iostream>
  2. #include <unordered_set>
  3.  
  4. struct Node {
  5.   std::string product_name;
  6.   int color;
  7.   int size;
  8.   bool operator==(const Node &other) const {
  9.     return product_name == other.product_name && color == other.color && size == other.size;
  10.   }
  11. };
  12.  
  13. template <> struct std::hash<Node> {
  14.   size_t operator()(const Node &key) const {
  15.     size_t h1 = std::hash<std::string> {} (key.product_name);
  16.     size_t h2 = std::hash<int> {} (key.color);
  17.     size_t h3 = std::hash<int> {} (key.size);
  18.     return h1 ^ (h2 << 1) ^ (h3 << 2);
  19.   }
  20. };
  21.  
  22. int main() {
  23.   std::unordered_set<Node> set;
  24.   char query;
  25.   std::string name;
  26.   int color;
  27.   int size;
  28.   while(std::cin >> query >> name >> color >> size) {
  29.     Node _new;
  30.     _new.product_name = name;
  31.     _new.color = color;
  32.     _new.size = size;
  33.     switch(query) {
  34.       case '+':
  35.         if(set.insert(_new).second)
  36.           std::cout << "OK\n";
  37.         else
  38.           std::cout << "FAIL\n";
  39.         break;
  40.       case '-':
  41.         if(set.erase(_new))
  42.           std::cout << "OK\n";
  43.         else
  44.           std::cout << "FAIL\n";
  45.         break;
  46.       case '?':
  47.         if(set.count(_new))
  48.           std::cout << "OK\n";
  49.         else
  50.           std::cout << "FAIL\n";
  51.     }
  52.   }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement