Advertisement
MeehoweCK

Untitled

Apr 24th, 2024
476
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.92 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <forward_list>
  4. #include <functional>
  5. #include <algorithm>
  6.  
  7. std::vector<int> dodajParzyste() {
  8.     std::cout << "Dodaje parzyste\n";
  9.     std::vector<int> wynik{ 2, 4, 6 };
  10.     return wynik;
  11. }
  12.  
  13. std::vector<int> dodajNieparzyste() {
  14.     std::cout << "Dodaje nieparzyste\n";
  15.     std::vector<int> wynik{ 1, 3, 5 };
  16.     return wynik;
  17. }
  18.  
  19. template <typename Container>
  20. void addElementsToContainer(Container& existing, Container&& added) {
  21.     std::move(added.begin(), added.end(), std::back_inserter(existing));
  22. }
  23.  
  24. int main() {
  25.     std::forward_list<std::function<std::vector<int>()>> funkcje{};
  26.     funkcje.push_front(dodajParzyste);
  27.     funkcje.push_front(dodajNieparzyste);
  28.  
  29.     std::vector<int> liczby{};
  30.     std::for_each(funkcje.begin(), funkcje.end(), [&liczby](const auto& func) {
  31.         addElementsToContainer(liczby, func());
  32.         });
  33.  
  34.     for (auto x : liczby) {
  35.         std::cout << x << '\n';
  36.     }
  37.     return 0;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement