Last active
August 29, 2015 14:13
-
-
Save zaltoprofen/a1e6a654956500dc9a62 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <type_traits> | |
| #include <iterator> | |
| #include <vector> | |
| #include <utility> | |
| template<typename Function, typename InputIterator> | |
| auto mapping(Function f, InputIterator begin, InputIterator end) | |
| -> std::vector< | |
| typename std::result_of<Function(typename std::iterator_traits<InputIterator>::value_type)>::type | |
| > | |
| { | |
| typedef typename std::result_of<Function(typename std::iterator_traits<InputIterator>::value_type)>::type | |
| result_value_type; | |
| std::vector<result_value_type> retval; | |
| for(;begin != end; ++begin){ | |
| retval.push_back(f(*begin)); | |
| } | |
| return retval; | |
| } | |
| int main(int argc, char const* argv[]) | |
| { | |
| int dat[] = {1, 2, 3, 4, 5}; | |
| auto ret = mapping([](int x){ return x * x; }, dat, dat + 5); | |
| for (auto&& var : ret) { | |
| std::cout << var << std::endl; | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment