Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <iostream>
- #include <vector>
- #include <iomanip>
- #include <functional>
- #include <iterator>
- template<typename Collection, typename unop>
- void for_each(Collection col, unop op) {
- std::for_each(col.begin(), col.end(), op);
- }
- template<typename Collection, typename unop>
- Collection map(Collection col, unop op) {
- std::transform(col.begin(), col.end(), col.begin(), op);
- return col;
- }
- int main()
- {
- auto println = [](const char *message) { std::cout << "\n" << message << '\n'; };
- auto lambda_echo = [](int i) { std::cout << std::setw(3) << i; };
- std::vector<int> col{ 23, 23, 37, 42, 23, 23, 37 };
- // call map
- println("running map<T> operation");
- auto add_one = [](int i) { return i + 1; };
- auto new_col = map(col, add_one);
- for_each(col, lambda_echo);
- return 0;
- }
Add Comment
Please, Sign In to add comment