Skip to content

Instantly share code, notes, and snippets.

@ErnestasJa
Last active January 22, 2016 23:58
Show Gist options
  • Save ErnestasJa/f8d469236d9917f8a49f to your computer and use it in GitHub Desktop.
Save ErnestasJa/f8d469236d9917f8a49f to your computer and use it in GitHub Desktop.
// Example program
#include <iostream>
#include <string>
#include <vector>
#include <type_traits>
struct point
{
float x, y, z;
};
struct point2D
{
float x, y;
};
template <class T, typename MapFunctor>
auto map(const std::vector<T> & inVector, const MapFunctor & functor)
{
typedef typename std::result_of< MapFunctor(const T &) >::type ReturnType;
std::vector<ReturnType> outVector;
for(const T & data : inVector)
outVector.push_back(functor(data));
return outVector;
}
int main()
{
std::vector<point> points = {{1.f,2.f,3.f}};
auto coords = map(points, [](const point & p)->point2D
{
return point2D{p.x, p.y};
});
for(auto data : coords)
std::cout<< data.x << " " << data.y << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment