I am trying to write an iterator adaptor that should call a member function (or access a member of the object) each time it is dereferenced. Here is an example of such an API:
vector<pair<int,int>> ps = {{1,"a"}, {2,"b"}, {3,"c"}};
// Pairs that represent ranges
auto rf = make_adaptor(ps.begin(), ps.end(), [](const auto& x) {return x.first;}
auto rs = make_adaptor(ps.begin(), ps.end(), [](auto& x) {return x.second;}
Should print out 123
:
for_each(rf.first, rf.second, [](const auto& x){std::cout << x;});
Should set every second
element of the pairs in ps
:
for_each(rs.first, rs.second, [](auto& x){ x = "hello";});
I have tried writing an own iterator
type together with a make_adaptor
method, but I can't seem to get it to work:
template <typename Iterator, typename UnaryOp>
struct adaptor {
using value_type = std::result_of<UnaryOp(typename Iterator::reference)>::type;
using reference = value_type&;
using pointer = value_type*;
using difference_type = typename Iterator::difference_type;
using iterator_category = typename Iterator::iterator_category;
adaptor(){};
adaptor(Iterator it, UnaryOp func) : _it(it), _func(func) {}
reference operator*() const { return _func(*_it); }
pointer operator->() const { return &_func(*_it); }
bool operator==(const adaptor& other) const { return _it == other._it; }
bool operator!=(const adaptor& other) const { return _it != other._it; }
adaptor& operator++() {
++_it;
return *this;
}
Iterator _it;
UnaryOp _func;
};
template <typename Iterator, typename UnaryOp>
auto make_adaptor(Iterator first, Iterator last, UnaryOp func) {
return std::make_pair(adaptor<Iterator, UnaryOp>(first, func),
adaptor<Iterator, UnaryOp>(last, func));
};
The reason for this is the following: Let's say I have an algorithm convex_hull
which works on points
. But now I have objects which contains points
as a member (struct A { points pos;};
). I want to call convex_hull
on a collection of A
s.