Классический цикл for больше не нужен
От: Шахтер Интернет  
Дата: 30.06.18 15:52
Оценка: 1 (1) +1 -1 :))) :)))
Его можно заменить

/* main.cpp */ 

#include <iostream>

using namespace std;

/* struct Empty */

struct Empty
 {
 };

/* class IntRange */

class IntRange
 {
   int ind;
   int lim;

  public:

   IntRange(int ind_,int lim_) : ind(ind_),lim(lim_) {}

   // loop

   bool operator != (Empty) const { return ind<lim; }

   int operator * () const { return ind; }

   void operator ++ () { ind++; }

   // begin()/end()

   IntRange begin() const { return *this; }

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

/* main() */

int main()
 {
  // for(int i=0; i<10 ;i++)

  for(int i : IntRange(0,10) ) cout << i << ' ' ;

  cout << endl << endl ;

  return 0;
 }
В XXI век с CCore.
Копай Нео, копай -- летать научишься. © Matrix. Парадоксы
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.