Now, let's generalize this functionality into something usable with for-loops by wrapping it into iterators and a range. This way, we can utilize it as a first class citizen of C++ in both STL algorithms and range-based for-loop. The following table demonstrate this property:
Linear Range and an STL algorithm | Linear Range and a range based for-loop |
auto r =LinearRange<float>{0,1,4};auto vec =std::vector<float>{};std::copy( r.begin(), r.end(), std::back_inserter(vec));// vec is {0.0, 0.33, 0.66, 1.0} |
auto r =LinearRange<float>{0,1,4};auto vec =std::vector<float>{};for(auto t: r) { vec.push_back(t);}// vec is {0.0, 0.33, 0.66, 1.0} |
First, we need an iterator that is aware of the start value, ...