Игаль *    (yigal_s) wrote,
Игаль *   
yigal_s

Лёгкая итерация по числовому интервалу

class IntRange
{
		int mBegin;
		int mEnd;
public:
	IntRange(int iBegin, int iEnd)
		: mBegin(iBegin), mEnd(iEnd)
	{	}
	enum class iterator : int {	iMin = INT_MIN, iMax = INT_MAX};
	iterator begin() { return iterator(mBegin);}
	iterator end() { return iterator(mEnd);}
};

IntRange::iterator operator ++(IntRange::iterator& x) 
{
	return x = (IntRange::iterator)(int(x) + 1); 	
}

IntRange::iterator operator ++(IntRange::iterator& x, int /*postfix*/) 
{ 
	IntRange::iterator xx = x;
	x = (IntRange::iterator)(int(x) + 1);
	return xx;
}

int operator*(IntRange::iterator x) { return int(x); }

int _tmain(int argc, _TCHAR* argv[])
{
	for (int i : IntRange(0,10))
		printf("%d\n", i);
	return 0;
}


Crosspost: dreamwidth.org
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your IP address will be recorded 

  • 11 comments