I have an array which is not necessarily full.
It can be very sparse.
Is there a good way to iterate through this array without visiting all the possible indexes? (c++ array iterator?)
Or, even if I use array iterator, will it be nothing different from visiting every indexes and checking the value?
Yes, if you use an iterator, it’s the same as visiting every index and checking the value, and there’s no good way to skip logical holes. You could keep a list of good indices, but if you did that then why not just use a list to store the data in the first place?
If your data is very sparse, perhaps a better data structure would be a
std::map, or even anstd::unordered_map, depending on your application. These have decent lookup time while not wasting much space, like an array would have to.