c++11for-loopfor-range

Literal for ranges in C++11


If I have this:

for (auto iSong = 1; iSong <= iMaxSongNumber; iSong++)

Can I use the new for range approach?

I understand that for containers they need a begin and end method for them to work. But if we have literal max values?


Solution

  • There isn't a built-in mechanism to do this: range-based for works on something for which begin and end can be called.

    I wrote a blog post about how to do this: https://www.justsoftwaresolutions.co.uk/cplusplus/generating_sequences.html

    Basically, you need to create a "virtual container" with iterators that update the count.