简体   繁体   中英

How to create input iterator over sequence of function results in C++?

I have a function that gets a pair of input iterators:

template<typename I>
void foo(I begin, I end) {...}

I'd like to feed it with sequence generated by function - for example a sequence of random numbers. Is there any standard library mechanism to achieve this without necessity to store the sequence in a collection and then take iterators from collection?

You will need to build that yourself. Here is an example of how to do it:

template<typename F>
class function_iterator {
    using function = F;
public:
    using value_type = std::invoke_result_t<function>;
    using difference_type = std::ptrdiff_t;
    using reference = value_type const&;
    using pointer = value_type const*;
    using iterator_category = std::input_iterator_tag;

    function_iterator(function f, difference_type pos = 0);

    auto operator*() const -> value_type;
    auto operator++() -> function_iterator&;
    auto operator++(int) -> function_iterator;

private:
    function f_;
    difference_type pos_;

    template<typename T_>
    friend bool operator==(function_iterator<T_> const& lhs, function_iterator<T_> const& rhs);
    template<typename T_>
    friend bool operator!=(function_iterator<T_> const& lhs, function_iterator<T_> const& rhs);
};

template<typename T>
function_iterator<T>::function_iterator(function f, difference_type pos) :
f_(f), pos_(pos) {
}

template<typename F>
auto function_iterator<F>::operator*() const -> value_type {
    return f_();
}
template<typename F>
auto function_iterator<F>::operator++() -> function_iterator& {
    ++pos_;
    return *this;
}
template<typename F>
auto function_iterator<F>::operator++(int) -> function_iterator {
    auto it = *this;
    ++*this;
    return it;
}

template<typename T_>
bool operator==(function_iterator<T_> const& lhs, function_iterator<T_> const& rhs) {
    return lhs.pos_ == rhs.pos_;
}
template<typename T_>
bool operator!=(function_iterator<T_> const& lhs, function_iterator<T_> const& rhs) {
    return !(lhs == rhs);
}

Live

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM