Is there a range class in C++11 for use with range based for loops?

前端 未结 8 1293
失恋的感觉
失恋的感觉 2020-11-27 11:33

I found myself writing this just a bit ago:

template 
class range_class {
 public:
   class iterator {
      friend c         


        
8条回答
  •  爱一瞬间的悲伤
    2020-11-27 11:55

    I found that boost::irange was much slower than the canonical integer loop. So I settled on the following much simpler solution using a preprocessor macro:

    #define RANGE(a, b) unsigned a=0; a

    Then you can loop like this:

    for(RANGE(i, n)) {
        // code here
    }
    

    This range automatically starts from zero. It could be easily extended to start from a given number.

提交回复
热议问题