Iterator usage example

Now this linear range iterator can be used without having it wrapped into a range. In the following code two standalone iterators representing a range are created:

auto start = 0.0f;auto stop = 1.0f;auto num_values = size_t{6};auto step_size = get_step_size(start, stop, num_values);auto first = LinearRangeIterator<float>{start, step_size, 0};auto last = LinearRangeIterator<float>{start, step_size, num_values};

These two iterators can now be utilized in a regular for-loop, or an STL algorithm as shown in the following table:

Copy into std::set using a for-loop Copy into std::set using std::copy
auto s = std::set<float>{};for(auto it=first; it!=last; ++it){  s.insert(*it);}
auto s = std::set<float>{};auto dst = ...

Get C++ High Performance now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.