Как я могу убедиться, что итератор не go выходит за пределы вектора диапазона? и в случае, когда фильтр не будет соответствовать ни одному из элементов ввода; в таком случае, как я могу убедиться, что begin () равно end () Code
template<typename Iterator>
struct Range {
using LazyIterator = Iterator; // required for accessing the used Iterator type from other locations
Iterator m_begin;
Iterator m_end;
auto begin() { return m_begin; }
auto end() { return m_end; }
};
struct FilteringIterator : Iterator {
Callable callable;
using OriginalIterator = Iterator;
Iterator end;
Iterator current=(Iterator &)*this;;
FilteringIterator(const Iterator begin, Iterator end, Callable callable):Iterator(begin),end(end),callable(callable){}
Iterator &get_orig_iter() { return ((Iterator &)*this); }
auto operator++(){ return ((Iterator &)*this)++;}
bool operator==(const Iterator& rhs) const { return ((Iterator &)*this) == rhs; }
bool operator!=(const Iterator& rhs) const { return !(operator==(rhs)); }
auto operator*() {
while ((Iterator &)*this!=end && !callable(*get_orig_iter()))
++(Iterator &)*this;
return *(Iterator &)*this;
}
};
auto filter = [](auto action) {
return [=]( auto &container) {
using Container = std::decay_t<decltype(container)>;
using Iterator = typename Container::iterator;
using actiontype = decltype(action);
using filter_iterator = FilteringIterator<Iterator, actiontype>;
auto t=filter_iterator{container.end(),container.end(),action};
return Range{filter_iterator{container.begin(),container.end(),action}, filter_iterator{container.end(),container.end(),action}};
};
};
Main
for(int i=0; i<5; ++i)
v.push_back(odd_gen() * 2.5);
// v contains {2.5, 7.5, 12.5, 17.5, 22.5} here
for(auto a : v | filter(less_then(15))) // filter is applied lazily as the range is traversed
std::cout << a <<"D" << std::endl;
// prints 17.5 and 22.5 to the console
Output
2.5
7.5
12.5
Ошибка