In the code below I am contrasting and comparing for-each loop situations on a normal array. Do the for-each() loops below appear "legal"? Am I using them appropriately? I ask becuase I can get my output from either elem deployment or the array itself(e.g. objects[elem-1]). Seems like 2 different ways from 1 function. Is this correct?
ALso does the for-each start with when scanning an array from array[0] or 1? I had a one-off error with objects[elem-1]. See COUT ELEM & COUT OBJECTS
What index does the for-each start with when scanning an array - 0 or 1. I had a one-off error with objects[elem-1].
Because that makes no sense and only works in this case because the elem happens to be an int, and you are then using the value of elem to reach into the array again, which is completely missing the point of the ranged for loop.
The whole point of the ranged for loop is that you don't care about keeping count, and you don't reach into the array yourself. What index does it start from? irrelevant. It starts at the start, and goes through to the end.
You had your one-off error because you put the number 1 into element 0, but that's just complete chance. If you'd put the number 50 million into it, you'd then have tried to read objects[50 million].
If it was an array of char-pointers to animal names, you'd have tried to read objects["kittens"], and how well would that have worked out?
Ok, in the following example, to be starkly obvious I just made card_deck array of type Card_Deck. If I try to have it print out card_deck[elem].I did get lucky with ints. I get the error you mentioned above. What's cool is the container did not need to know length from me and other factors. I'll look up the other features.
code using for-each loop correctly(or so I believe!)