Literal for ranges in C++11

149 Views Asked by At

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?

1

There are 1 best solutions below

2
On BEST ANSWER

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.