Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdint>
- #include <vector>
- #include <memory>
- #include <algorithm>
- #include <random>
- #include <iostream>
- template <typename T>
- class Foo {
- public:
- Foo(size_t bignum) : _data(_make_some_data(bignum)) {}
- ~Foo() = default;
- void print_it() const {
- for (auto element : _data) std::cout << element << "\n";
- }
- T sum() const {
- T sum = 0;
- for (auto n : _data) sum += n;
- return sum;
- }
- private:
- std::vector<T> _data;
- static std::vector<T> _make_some_data(size_t lots_of_data) {
- std::random_device generator;
- std::uniform_int_distribution<T> distribution(0, 100);
- std::vector<T> new_vec;
- for (size_t n = 0; n < lots_of_data; ++n) {
- new_vec.push_back(distribution(generator));
- }
- return new_vec;
- }
- };
- template <typename T>
- class Bar {
- public:
- Bar(size_t lotsa_classes, size_t lotsa_data) {
- for (size_t n = 0; n < lotsa_classes; ++n) {
- _class_data.push_back(std::unique_ptr<Foo<T>>(new Foo<T>(lotsa_data)));
- }
- }
- ~Bar() = default;
- void print_the_best() {
- if (_the_best_Foo) {
- _the_best_Foo->print_it();
- }
- }
- void get_the_best() {
- std::sort(
- _class_data.begin(), _class_data.end(),
- [](const std::unique_ptr<Foo<T>>& a, const std::unique_ptr<Foo<T>>& b) {
- return a->sum() > b->sum();
- });
- _the_best_Foo = std::move(_class_data[0]);
- _class_data.erase(_class_data.begin());
- }
- private:
- std::vector<std::unique_ptr<Foo<T>>> _class_data;
- std::unique_ptr<Foo<T>> _the_best_Foo;
- };
- int main() {
- Bar<uint16_t> bar(100, 10);
- bar.get_the_best();
- bar.print_the_best();
- }
Advertisement
Add Comment
Please, Sign In to add comment