Advertisement
oster_man

Hashable

Aug 22nd, 2023
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.23 KB | None | 0 0
  1. #include <array>
  2. #include <iomanip>
  3. #include <iostream>
  4. #include <optional>
  5. #include <sstream>
  6. #include <string>
  7. #include <vector>
  8. #include <algorithm>
  9.  
  10. using namespace std;
  11.  
  12. class VehiclePlate {
  13. public:
  14.     VehiclePlate(char l0, char l1, int digits, char l2, int region)
  15.         : letters_{l0, l1, l2}
  16.         , digits_(digits)
  17.         , region_(region) {
  18.     }
  19.  
  20.     string ToString() const {
  21.         ostringstream out;
  22.         out << letters_[0] << letters_[1];
  23.         // чтобы дополнить цифровую часть номера слева нулями
  24.         // до трёх цифр, используем подобные манипуляторы:
  25.         // setfill задаёт символ для заполнения,
  26.         // right задаёт выравнивание по правому краю,
  27.         // setw задаёт минимальное желаемое количество знаков
  28.         out << setfill('0') << right << setw(3) << digits_;
  29.         out << letters_[2] << setw(2) << region_;
  30.  
  31.         return out.str();
  32.     }
  33.  
  34.     int Hash() const {
  35.         return digits_;
  36.     }
  37.  
  38.     bool operator==(const VehiclePlate& other)
  39.     {
  40.         return other.digits_==this->digits_ && other.region_==this->region_ && other.letters_==this->letters_;
  41.     }
  42.  
  43. private:
  44.     array<char, 3> letters_;
  45.     int digits_;
  46.     int region_;
  47. };
  48.  
  49. ostream& operator<<(ostream& out, VehiclePlate plate) {
  50.     out << plate.ToString();
  51.     return out;
  52. }
  53.  
  54. template<class T>
  55. bool operator==(const T& lhs,const T& rhs)
  56.     {
  57.         return lhs==rhs;
  58.     }
  59.  
  60. template<class T>
  61. bool operator!=(const T& lhs,const T& rhs)
  62.     {
  63.         return !(lhs==rhs);
  64.     }
  65.  
  66. template <typename T>
  67. class HashableContainer {
  68. public:
  69.     void Insert(T elem) {
  70.         int index = elem.Hash();
  71.        
  72.         if (index >= int(elements_.size())) {
  73.             elements_.resize(index * 2 + 1);
  74.         }
  75.  
  76.         if (std::find(elements_[index].begin(),elements_[index].end(),elem)==elements_[index].end())
  77.             elements_[index].push_back(move(elem));
  78.         else
  79.             return;
  80.     }
  81.  
  82.     void PrintAll(ostream& out) const {
  83.         for (auto& e : elements_) {
  84.             // if (e.empty()) {
  85.             //     continue;
  86.             // }
  87.             for(auto& number:e)
  88.             out << number << endl;
  89.         }
  90.     }
  91.  
  92.     const auto& GetVector() const {
  93.         return elements_;
  94.     }
  95.  
  96. private:
  97.     std::vector<vector<T>> elements_;
  98. };
  99.  
  100. int main() {
  101.     HashableContainer<VehiclePlate> plate_base;
  102.     plate_base.Insert({'B', 'H', 840, 'E', 99});
  103.     plate_base.Insert({'O', 'K', 942, 'K', 78});
  104.     plate_base.Insert({'O', 'K', 942, 'K', 78});
  105.     plate_base.Insert({'O', 'K', 942, 'K', 78});
  106.     plate_base.Insert({'O', 'K', 942, 'K', 78});
  107.     plate_base.Insert({'H', 'E', 968, 'C', 79});
  108.     plate_base.Insert({'T', 'A', 326, 'X', 83});
  109.     plate_base.Insert({'H', 'H', 831, 'P', 116});
  110.     plate_base.Insert({'P', 'M', 884, 'K', 23});
  111.     plate_base.Insert({'O', 'C', 34, 'P', 24});
  112.     plate_base.Insert({'M', 'Y', 831, 'M', 43});
  113.     plate_base.Insert({'K', 'T', 478, 'P', 49});
  114.     plate_base.Insert({'X', 'P', 850, 'A', 50});
  115.  
  116.     plate_base.PrintAll(cout);
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement