Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. //
  2. // Created by Piotr Moszkowicz on 2019-06-16.
  3. //
  4.  
  5. #include "all_need.h"
  6.  
  7. template<typename T, typename Func>
  8. void my_for_each(T* begin, T* end, std::function<void(T*)> fun) {
  9.     T* iter = begin;
  10.     while (iter++ != end) {
  11.         fun(iter);
  12.     }
  13. }
  14.  
  15. using namespace std;
  16.  
  17. int main() {
  18.     int c[] = {1, 2, 3, 4, 5};
  19.  
  20.     auto print = [](int* a) {
  21.         std::cout << *a << " ";
  22.     };
  23.  
  24.     auto print_eol = []() {
  25.         std::cout << std::endl;
  26.     };
  27.  
  28.     my_for_each(begin(c), end(c), print);
  29.     print_eol();
  30.  
  31.     int add_value = 1;
  32.     auto add = [&add_value](int* a) {
  33.         (*a) += add_value;
  34.     };
  35.  
  36.     my_for_each(begin(c), end(c), add);
  37.     my_for_each(begin(c), end(c), print);
  38.     print_eol();
  39.  
  40.     add_value = 10;
  41.  
  42.     my_for_each(begin(c), end(c), add);
  43.     my_for_each(begin(c), end(c), print);
  44.     print_eol();
  45.  
  46.     return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement