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

前端 未结 8 1253
失恋的感觉
失恋的感觉 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<b; 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.

    0 讨论(0)
  • 2020-11-27 11:59

    The C++ standard library does not have one, but Boost.Range has boost::counting_range, which certainly qualifies. You could also use boost::irange, which is a bit more focused in scope.

    C++20's range library will allow you to do this via view::iota(start, end).

    0 讨论(0)
提交回复
热议问题