Advertisement
kutuzzzov

Урок 4

Feb 16th, 2023
1,463
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.28 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) const {
  39.         return (this->letters_ == other.letters_) &&
  40.             (this->digits_ == other.digits_) &&
  41.             (this->region_ == other.region_);
  42.     }
  43.  
  44. private:
  45.     array<char, 3> letters_;
  46.     int digits_;
  47.     int region_;
  48. };
  49.  
  50. ostream& operator<<(ostream& out, VehiclePlate plate) {
  51.     out << plate.ToString();
  52.     return out;
  53. }
  54.  
  55. template <typename T>
  56. class HashableContainer {
  57. public:
  58.     void Insert(T elem) {
  59.         int index = elem.Hash();
  60.  
  61.         // если вектор недостаточно велик для этого индекса,
  62.         // то увеличим его, выделив место с запасом
  63.         if (index >= int(elements_.size())) {
  64.             elements_.resize(index * 2 + 1);
  65.         }
  66.        
  67.         auto it = find(elements_[index].begin(), elements_[index].end(), elem);
  68.         if (elements_[index].end() == it) {
  69.             elements_[index].push_back(move(elem));
  70.         }
  71.     }
  72.  
  73.     void PrintAll(ostream& out) const {
  74.         for (auto& e : elements_) {
  75.             /*if (!elements_.empty()) {
  76.                 continue;
  77.             }*/
  78.             for (auto& i : e) {
  79.                 out << i << endl;
  80.             }
  81.         }
  82.     }
  83.  
  84.     const auto& GetVector() const {
  85.         return elements_;
  86.     }
  87.  
  88. private:
  89.     vector<vector<T>> elements_;
  90. };
  91.  
  92. int main() {
  93.     HashableContainer<VehiclePlate> plate_base;
  94.     plate_base.Insert({ 'B', 'H', 840, 'E', 99 });
  95.     plate_base.Insert({ 'O', 'K', 942, 'K', 78 });
  96.     plate_base.Insert({ 'O', 'K', 942, 'K', 78 });
  97.     plate_base.Insert({ 'O', 'K', 942, 'K', 78 });
  98.     plate_base.Insert({ 'O', 'K', 942, 'K', 78 });
  99.     plate_base.Insert({ 'H', 'E', 968, 'C', 79 });
  100.     plate_base.Insert({ 'T', 'A', 326, 'X', 83 });
  101.     plate_base.Insert({ 'H', 'H', 831, 'P', 116 });
  102.     plate_base.Insert({ 'P', 'M', 884, 'K', 23 });
  103.     plate_base.Insert({ 'O', 'C', 34, 'P', 24 });
  104.     plate_base.Insert({ 'M', 'Y', 831, 'M', 43 });
  105.     plate_base.Insert({ 'K', 'T', 478, 'P', 49 });
  106.     plate_base.Insert({ 'X', 'P', 850, 'A', 50 });
  107.  
  108.     plate_base.PrintAll(cout);
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement