Last active
January 22, 2016 23:58
-
-
Save ErnestasJa/f8d469236d9917f8a49f 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
// 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