for_each () disambiguation - 2 different ways from one function?

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include<iostream>
#include<array>

int main()
{

	int ctr(0);
		int objects[52] = {};			//normal array
		for (int i = 0; i<4;++i)
		{

	for (int j = 1; j<14;++j)
		{

		objects[ctr] = j;
		ctr=ctr+1;

		}

		}
//COUT ELEM
std::cout<<std::endl;
for(auto &elem:objects)			//can be iterated over by for-each loop (elem in this case)
{								 
	std::cout<<elem;
}

std::cout<<std::endl;
std::cout<<std::endl;
//COUT OBJECTS[]
for(auto &elem:objects)
{
	std::cout<<objects[elem-1];
}
ctr = 0;



		return 0;
}


12345678910111213123456789101112131234567891011121312345678910111213

12345678910111213123456789101112131234567891011121312345678910111213
Last edited on
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?
Last edited on
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!)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include<iostream>
#include<array>

struct Card_Objects{
	int ranks = 0;
	int suits = 0;
};

int main()
{
			int ctr(0);
			Card_Objects card_objects[52] = {};			//normal array
			for (int suits = 1; suits<5;++suits)
			{

			for (int ranks = 2; ranks<15; ++ranks)
			{

			card_objects[ctr].ranks = ranks;
			card_objects[ctr].suits = suits;
			ctr=ctr+1;
			}
			}
			//readback
			std::cout<<"Suits"<<"\tRanks"<<std::endl;
			for (auto &elem:card_objects)
			{
			std::cout<<elem.suits<<"\t"<<elem.ranks<<std::endl;
			
			}

	return 0;
}


Suits	Ranks
1	2
1	3
1	4
1	5
1	6
1	7
1	8
1	9
1	10
1	11
1	12
1	13
1	14
2	2
2	3
2	4
2	5
2	6
2	7
2	8
2	9
2	10
2	11
2	12
2	13
2	14
3	2
3	3
3	4
3	5
3	6
3	7
3	8
3	9
3	10
3	11
3	12
3	13
3	14
4	2
4	3
4	4
4	5
4	6
4	7
4	8
4	9
4	10
4	11
4	12
4	13
4	14
Last edited on
Topic archived. No new replies allowed.