Simple customized iterator with lambdas in C++
Suppose I have a container which contains int, a function that works over
containers containing Point, and that I have a function that given some
int gives me the corresponding Point it represents (imagine that I have
indexed all the points in my scene in some big std::vector<Point>). How do
I create a simple (and efficient) wrapper to use my first container
without copying its content?
The code I want to type is something like that:
template<typename InputIterator>
double compute_area(InputIterator first, InputIterator beyond) {
// Do stuff
}
template<typename InputIterator, typename OutputIterator>
void convex_hull(InputIterator first, InputIterator beyond, OutputIterator
result) {
// Do stuff
}
struct Scene {
std::vector<Point> vertices;
foo(const std::vector<int> &polygon) {
// Create a simple wraper with limited amount of mumbo-jumbo
auto functor = [](int i) -> Point& { return vertices[polygon[i]]; });
MagicIterator polyBegin(0, functor);
MagicIterator polyEnd(polygon.size(), functor);
// NOTE: I want them to act as random access iterator
// And then use it directly
double a = compute_area(polyBegin, polyEnd);
// Bonus: create custom inserter similar to std::back_inserter
std::vector<int> result;
convex_hull(polyBegin, polyEnd, MagicInserter(result));
}
};
So, as you've seen, I'm looking for something a bit generic. I thought
about using lambdas as well, but I'm getting a bit mixed up on how to
proceed to keep it simple and user-friendly.
No comments:
Post a Comment