Advertisement
MeehoweCK

Untitled

Apr 24th, 2024
508
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.23 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. std::vector<int> dodajKolejne(int a, int b) {
  20.     std::cout << "Dodaje liczby od " << a << " do " << b << std::endl;
  21.     std::vector<int> result{};
  22.     for (auto i{ a }; i <= b; ++i) {
  23.         result.push_back(i);
  24.     }
  25.     return result;
  26. }
  27.  
  28. template <typename Container>
  29. void addElementsToContainer(Container& existing, Container&& added) {
  30.     std::move(added.begin(), added.end(), std::back_inserter(existing));
  31. }
  32.  
  33. int main() {
  34.     std::forward_list<std::function<std::vector<int>()>> funkcje{};
  35.     funkcje.push_front(dodajParzyste);
  36.     funkcje.push_front(dodajNieparzyste);
  37.     auto funkcja{ std::bind(dodajKolejne, 12, 20) };
  38.     funkcje.push_front(funkcja);
  39.  
  40.     std::vector<int> liczby{};
  41.     std::for_each(funkcje.begin(), funkcje.end(), [&liczby](const auto& func) {
  42.         addElementsToContainer(liczby, func());
  43.         });
  44.  
  45.     for (auto x : liczby) {
  46.         std::cout << x << '\n';
  47.     }
  48.     return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement