2

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?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164

1 Answers1

1

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.

Anthony Williams
  • 66,628
  • 14
  • 133
  • 155
  • Thanks for the information you have provided. I don't think it is worth the effort for my needs. If there was something out of the box ... Also, in my instances I also need 1 stated indexing as opposed to 0 based. But thank you anyway. I will accept this answer. – Andrew Truckle Mar 20 '17 at 11:29
  • The final code linked to from the blog post allows 1-based indexing. https://www.justsoftwaresolutions.co.uk/files/numeric_range.cpp – Anthony Williams Mar 20 '17 at 11:56