vendredi 25 août 2017

How to write a function that iterates over anything that provides an iterator

In Python it's very easy to iterate over anything that provides an iterator:

def iterate(iterable):
    for i in iterable:
        do_something(i)

Is it possible to write a similiar function in C++?

Other answers suggest something like:

template<class iterator_type>
void iterate(iterator_type it, iterator_type end)
{
    while (it != end) {
        do_something(*(it++));
    }
}

But is there a way that can directly take something with an iterator and fetches the iterator begin and end by itself?

So after all, the question reduces to: Is there a standardized way to get an iterator from any object that provides an iterator? (Like iter(...) in Python.)

Aucun commentaire:

Enregistrer un commentaire