how to retrieve element "number"

In the code below I can print members of elem (.x .y). How do I print the index number (for lack of better words)of elem itself (similar to index of a normal matrix)?

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
41
42
43
44
45
46
#include<iostream>
#include<array>

using namespace std;

struct objects
{
	int x=0;
	int y=0;
};

void fill_array(array<objects,10> &my_array)
{

		int ctr = 1;
		for( objects& elem:my_array)
		{
			elem.x = ctr*4;
			elem.y = ctr*3;
			ctr++;
		}
}
void print_array(array<objects, 10>&my_array)
{
		for(objects& elem:my_array)
		{
			cout<<elem.x<<"\t"<<elem.y<<endl;
		}
}




int main()
{
	array<objects, 10>my_array;
	fill_array(my_array);
	print_array(my_array);

	return 0;
}




You could use a Counter variable (like you do in the other function), or do an ordinary for loop:

1
2
3
4
5
6
7
8
9
10
void print_array(const array<objects, 10>& my_array)
{
    std::size_t Counter = 0;
    for(const auto& elem: my_array)
    {
	std::cout << elem.x << "\t" << elem.y;
        std::cout << " Counter " << Counter << '\n';
        Counter++;
    }
}


If you configure your editor to translate tabs into 4 spaces say, your code will display better. This site transforms tabs into 8 spaces.
Perhaps something like:
1
2
3
4
5
6
7
8
9
void print_array(array<objects, 10>&my_array)
{
                int index = 0;
		for(objects& elem:my_array)
		{
			cout << "Index: " << index << " " << elem.x << "\t"<< elem.y << endl;
                        index++;
		}
}
But would have to always count up to the desired value. For example if I wanted to access members of elem 100, elem.x & elem.y, I would have to set up a loop. In other words my thinking that you can pluck a value out like an array is wrong.
You can do that with:
my_array[100].x /* or whatever */
The for loop is a looping construct, so if you don't want to loop you shouldn't use it.
Just use a normal for loop as opposed to a range based for loop. That way you'll have your index right there.
@Computergeek01

The OP wants to grab one item, as per how Zhuge explained?

@technologist

I recommend you look at the documentation for std::array, see all the different things you can do :+)

http://www.cplusplus.com/reference/array/array/


The subscript access is the operator[] , and there is also the at() function.
1
2
3
4
5
6
7
8
9
10
11
12
void fill_array(array<objects,10> &my_array)
{

		int ctr = 1;
		for( objects& elem:my_array)
		{

			elem[ctr].x = ctr*4;
			elem[ctr].y = ctr*3;
			ctr++;
		}
}

I looked over a lot of suggested documentation. I have down what to do with my_array but don't understand why you can access elem members but not the index to it itself.

I tried writing elem[index] = error.
OP wrote:
I tried writing elem[index] = error.

Take a minute and think about why this is OP. The variable 'elem' isn't an array, it's a reference to an element in 'my_array'. Think about why that is different.
Hi

I didn't know elem wasn't an array. Foolish to try to index it.

Seems like the elem and my_array have some overlapping functions despite one being a reference element to the array and the other being an array element. If I use my_array[i].x and my_array[i].y they are the same results as elem.x and elem.y. In the output the elem and my_array seem to be pairing data(overlap). I could use some disambiguation here on when to use one versus the other.

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
41
42
43
44
45
46
47
48
#include<iostream>
#include<array>

using namespace std;

struct objects
{
	int x=0;
	int y=0;
};

void fill_array(array<objects,10> &my_array)
{

		int ctr = 1;
		for( objects& elem:my_array)
		{
			my_array[ctr].x = ctr * 2;
			my_array[ctr].y = ctr*ctr;
			
			
			ctr++;
		}
}
void print_array(array<objects, 10>&my_array)
{
	int ctr(0);
		for(objects& elem:my_array)
		{
			++ctr;
			cout<<"elem"<<"\t\t"<<elem.x<<"\t"<<elem.y<<endl;
			cout<<"my_array"<<"\t"<<my_array[ctr].x<<"\t"<<my_array[ctr].y<<endl;
		}
}




int main()
{
	array<objects, 10>my_array;
	fill_array(my_array);
	print_array(my_array);

	return 0;
}




elem		0	0
my_array	2	1
elem		2	1
my_array	4	4
elem		4	4
my_array	6	9
elem		6	9
my_array	8	16
elem		8	16
my_array	10	25
elem		10	25
my_array	12	36
elem		12	36
my_array	14	49
elem		14	49
...
Last edited on
They are identical by design. The ranged-based for loop iterates over your array, giving you the items out of it. Similarly, going through the valid indices of an array and passing them to the subscript operator will give you the items out of it.

You should use whichever one makes more sense.

If you only want the items of the array (which I would wager a guess is the usual case), then use a range-based for loop. This also has the advantage of working for various other kinds of ranges that don't necessarily use the numeric subscript method.
1
2
3
4
for(auto elem : container) {
    // use elem
    // I don't need to care about the details of iterating container
}


If you need a counter for some other reason (perhaps you want to number a list or something), then you can declare a counter and do it that way.
1
2
3
4
5
for(unsigned int i = 0; i < container.size(); ++i) {
    auto elem = container[i];
    // now operate on elem
    // Note that here I have to know how to calculate the size of container, and also figure out how to access elements out of it.
}
Thx all...
Topic archived. No new replies allowed.