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?
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.
You can use this
std::for_each
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With