Skip to content

Instantly share code, notes, and snippets.

@zaltoprofen
Last active August 29, 2015 14:13
Show Gist options
  • Save zaltoprofen/a1e6a654956500dc9a62 to your computer and use it in GitHub Desktop.
Save zaltoprofen/a1e6a654956500dc9a62 to your computer and use it in GitHub Desktop.
#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