Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a class capable of foreach

It's been a while since Visual Studio added support for a foreach extension that works like

vector<int> v(3)
for each (int i in v) {
  printf("%d\n",i);
}

I want to know how to make any class able to use foreach. Do I need to implement some interface?

like image 939
stelonix Avatar asked Oct 26 '09 17:10

stelonix


2 Answers

for each statement in VC++, when used on a non-managed class:

for each (T x in xs)
{
    ...
}

is just syntactic sugar for this:

for (auto iter = xs.begin(), end = xs.end(); iter != end; ++iter)
{
     T x = *iter;
}

Where auto means that type of variable is deduced automatically from type of initializer.

In other words, you need to provide begin() and end() methods on your class that would return begin and end input iterators for it.

Here is an example of class that wraps an istream and allows you to iterate over all lines in it:

#include <istream>
#include <iostream>
#include <fstream>
#include <string>


class lines
{
public:

    class line_iterator
    {
    public:

        line_iterator() : in(0)
        {
        }

        line_iterator(std::istream& in) : in(&in)
        {
            ++*this;
        }

        line_iterator& operator++ ()
        {
            getline(*in, line);
            return *this;
        }

        line_iterator operator++ (int)
        {
            line_iterator result = *this;
            ++*this;
            return result;
        }

        const std::string& operator* () const
        {
            return line;
        }

        const std::string& operator-> () const
        {
            return line;
        }

        friend bool operator== (const line_iterator& lhs, const line_iterator& rhs)
        {
            return (lhs.in == rhs.in) ||
                   (lhs.in == 0 && rhs.in->eof()) ||
                   (rhs.in == 0 && lhs.in->eof());
        }

        friend bool operator!= (const line_iterator& lhs, const line_iterator& rhs)
        {
            return !(lhs == rhs);
        }

    private:

        std::istream* const in;
        std::string line;
    };


    lines(std::istream& in) : in(in)
    {
    }

    line_iterator begin() const
    {
        return line_iterator(in);
    }

    line_iterator end() const
    {
        return line_iterator();
    }

private:

    std::istream& in;
};


int main()
{
    std::ifstream f(__FILE__);
    for each (std::string line in lines(f))
    {
        std::cout << line << std::endl;
    }
}

Note that implementation of line_iterator is actually somewhat bigger than the minimum needed by for each; however, it is the minimum implementation that conforms to input iterator requirements, and thus this class is also usable with all STL algorithms that work on input iterators, such as std::for_each, std::find etc.

like image 110
Pavel Minaev Avatar answered Sep 19 '22 16:09

Pavel Minaev


You can use this

std::for_each

like image 21
Davit Siradeghyan Avatar answered Sep 19 '22 16:09

Davit Siradeghyan