Guest User

Untitled

a guest
Aug 25th, 2016
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.67 KB | None | 0 0
  1. #include <cstdint>
  2. #include <vector>
  3. #include <memory>
  4. #include <algorithm>
  5. #include <random>
  6. #include <iostream>
  7.  
  8. template <typename T>
  9. class Foo {
  10.  public:
  11.   Foo(size_t bignum) : _data(_make_some_data(bignum)) {}
  12.   ~Foo() = default;
  13.  
  14.   void print_it() const {
  15.     for (auto element : _data) std::cout << element << "\n";
  16.   }
  17.  
  18.   T sum() const {
  19.     T sum = 0;
  20.     for (auto n : _data) sum += n;
  21.     return sum;
  22.   }
  23.  
  24.  private:
  25.   std::vector<T> _data;
  26.  
  27.   static std::vector<T> _make_some_data(size_t lots_of_data) {
  28.     std::random_device generator;
  29.     std::uniform_int_distribution<T> distribution(0, 100);
  30.     std::vector<T> new_vec;
  31.  
  32.     for (size_t n = 0; n < lots_of_data; ++n) {
  33.       new_vec.push_back(distribution(generator));
  34.     }
  35.  
  36.     return new_vec;
  37.   }
  38. };
  39.  
  40. template <typename T>
  41. class Bar {
  42.  public:
  43.   Bar(size_t lotsa_classes, size_t lotsa_data) {
  44.     for (size_t n = 0; n < lotsa_classes; ++n) {
  45.       _class_data.push_back(std::unique_ptr<Foo<T>>(new Foo<T>(lotsa_data)));
  46.     }
  47.   }
  48.   ~Bar() = default;
  49.  
  50.   void print_the_best() {
  51.     if (_the_best_Foo) {
  52.       _the_best_Foo->print_it();
  53.     }
  54.   }
  55.  
  56.   void get_the_best() {
  57.     std::sort(
  58.         _class_data.begin(), _class_data.end(),
  59.         [](const std::unique_ptr<Foo<T>>& a, const std::unique_ptr<Foo<T>>& b) {
  60.           return a->sum() > b->sum();
  61.         });
  62.  
  63.     _the_best_Foo = std::move(_class_data[0]);
  64.     _class_data.erase(_class_data.begin());
  65.   }
  66.  
  67.  private:
  68.   std::vector<std::unique_ptr<Foo<T>>> _class_data;
  69.   std::unique_ptr<Foo<T>> _the_best_Foo;
  70. };
  71.  
  72. int main() {
  73.   Bar<uint16_t> bar(100, 10);
  74.   bar.get_the_best();
  75.   bar.print_the_best();
  76. }
Advertisement
Add Comment
Please, Sign In to add comment