Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Created by Piotr Moszkowicz on 2019-06-16.
- //
- #include "all_need.h"
- template<typename T, typename Func>
- void my_for_each(T* begin, T* end, std::function<void(T*)> fun) {
- T* iter = begin;
- while (iter++ != end) {
- fun(iter);
- }
- }
- using namespace std;
- int main() {
- int c[] = {1, 2, 3, 4, 5};
- auto print = [](int* a) {
- std::cout << *a << " ";
- };
- auto print_eol = []() {
- std::cout << std::endl;
- };
- my_for_each(begin(c), end(c), print);
- print_eol();
- int add_value = 1;
- auto add = [&add_value](int* a) {
- (*a) += add_value;
- };
- my_for_each(begin(c), end(c), add);
- my_for_each(begin(c), end(c), print);
- print_eol();
- add_value = 10;
- my_for_each(begin(c), end(c), add);
- my_for_each(begin(c), end(c), print);
- print_eol();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement