Advertisement
Guest User

фыы

a guest
Mar 25th, 2016
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.86 KB | None | 0 0
  1. #include <unordered_set>
  2. #include <iostream>
  3. #include <string>
  4. #include <vector>
  5. #include <algorithm>
  6. #include <iterator>
  7.  
  8. class Comparable {
  9. public:
  10.     int _weight;
  11.     std::string _name;
  12.     std::string _surname;
  13.  
  14.     Comparable(const int weight, const std::string &name, const std::string &surname) {
  15.         _weight = weight;
  16.         _name = name;
  17.         _surname = surname;
  18.     }
  19.  
  20.     Comparable(Comparable &&other) = default;
  21.     Comparable &operator=(const Comparable &other) = default;
  22.     Comparable &operator=(Comparable &&other) = default;
  23. };
  24.  
  25. class Hasher {
  26. public:
  27.     size_t operator() (const Comparable &c) const {
  28.         return c._name.size() + c._surname.size() << (c._weight & 0x0f);
  29.     }
  30. };
  31.  
  32. class SetComparator {
  33.  
  34. public:
  35.     enum class ComparatorType {
  36.         CompareByName,
  37.         CompareBySurname,
  38.         CompareByWeight
  39.     };
  40.  
  41.     SetComparator(ComparatorType type) {
  42.         _type = type;
  43.     }
  44.  
  45.     bool operator() (const Comparable &first, const Comparable &second) const {
  46.         switch (_type) {
  47.         case ComparatorType::CompareByName:
  48.             return first._name == second._name;
  49.             break;
  50.         case ComparatorType::CompareBySurname:
  51.             return first._surname == second._surname;
  52.             break;
  53.         case ComparatorType::CompareByWeight:
  54.             return first._weight == second._weight;
  55.             break;
  56.         }
  57.     }
  58.  
  59. private:
  60.     ComparatorType _type;
  61. };
  62.  
  63.  
  64. std::ostream &operator<<(std::ostream &stream, const Comparable &c) {
  65.     stream << c._name << " - " << c._surname << " - " << c._weight << std::endl;
  66.     return stream;
  67. }
  68.  
  69.  
  70. void main() {
  71.  
  72.     std::vector<Comparable> data{
  73.         Comparable(10, "Andrew", "Malkin"),
  74.         Comparable(12, "Ivan", "Gusakov"),
  75.         Comparable(10, "Shamil", "Huil"),
  76.         Comparable(15, "Ivan", "Chkalov"),
  77.         Comparable(15, "Maxim", "Huil")
  78.     };
  79.    
  80.     SetComparator nameComparator(SetComparator::ComparatorType::CompareByName);
  81.     SetComparator weightComparator(SetComparator::ComparatorType::CompareByWeight);
  82.  
  83.     Hasher hasher;
  84.  
  85.     std::unordered_set<Comparable, Hasher, SetComparator> set(20, hasher, nameComparator);
  86.     std::unordered_set<Comparable, Hasher, SetComparator> secondSet(20, hasher, weightComparator);
  87.  
  88.     set.max_load_factor(0.8f);
  89.     secondSet.max_load_factor(0.8f);
  90.  
  91.     std::cout << "Inserting first set: " << std::endl;
  92.     std::copy(data.cbegin(), data.cend(), std::inserter<decltype(set)>(set, set.begin()));
  93.     std::copy(set.cbegin(), set.cend(), std::ostream_iterator<Comparable>(std::cout));
  94.  
  95.     std::cout << "Inserting second set: " << std::endl;
  96.     std::copy(data.cbegin(), data.cend(), std::inserter<decltype(secondSet)>(secondSet, secondSet.begin()));
  97.     std::copy(secondSet.cbegin(), secondSet.cend(), std::ostream_iterator<Comparable>(std::cout));
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement