ranged based for in multidimensional array

What type does auto become in the following code?

1
2
3
4
5
6
7
8
int main()
{
    int arr[2][3] = { { 2, 3, 4 }, { 5, 6, 7} }; 

    for( auto &row : arr)
        for(auto &col : row)
            col = count ++;
}


I am trying to understand range based for loops in multidimensional arrays. auto works as a type, but int does not. I was thinking that maybe row is an array, but substituting it didn't work.

Edit:
Actually I think in my substitution I just missed placing a parenthesee, though I still don't understand how to print out the contents of the array if I am using another array to loop through it.
Last edited on
> I was thinking that maybe row is an array

auto& row : type of row is 'reference to array of 3 int'
(Since an array is not a copyable type, it can't be a value.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main()
{
    using inner_array_type = int[3] ; // 'inner_array_type' is an alias for 'array of 3 int'

    int arr[2][3] = { { 2, 3, 4 }, { 5, 6, 7} };
    // same as: */ inner_array_type arr[2] = { { 2, 3, 4 }, { 5, 6, 7} };

    // with type deduction
    for( auto& row : arr ) // type of row is 'reference to int[3]'
        for( auto& col : row) col = 7 ;

    // without type deduction
    for( inner_array_type& row : arr ) // type of row is 'reference to int[3]'
        for( int& col : row) col = 7 ;

    // verbose version without type deduction
    for( int (&row) [3] : arr ) // type of row is 'reference to int[3]'
        for( int& col : row) col = 7 ;
}
Thanks.
What does col = 7 do then? Isn't it basically the index for the row array? How might I use a range based for to change the values in a multidimensional array? I am used to having specific access to the incrementing index variables, but by making row an array, I am confused on how to do it.

This code is my failed attempt at trying to understand how to both use the array index to refer to the individual elements as well as show what those elements are. If I understand both of these things, I think I will have what I need to know.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
	std::array<std::array<int, 3>, 2>t;

	for (std::array<int, (sizeof t[0] / sizeof(int))>row : t)
	{
		for (int collumn : row)
		{
			std::cout << "t[" << row[collumn] << "][" << collumn
				<< "] = " << collumn << std::endl;
		}
		
	}
}
Last edited on
> What does col = 7 do then?

col is a poor name for the variable; I used the same name to keep in sync with your original code.

Perhaps this would make it clearer:
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
#include <iostream>

int main()
{
    double arr[2][3] = { { 2.3, 3.4, 4.6 }, { 5.7, 6.7, 7.8 } };

    const auto print = [&arr] // print the array arr
    {
        for( const auto& row : arr ) // for each row in the array
        {
            // print out the values in the row
            for( double value : row ) std::cout << value << "  " ;
            std::cout << '\n' ;
        }
        std::cout << "-----------------\n" ;
    };

    print() ;

    for( auto& row : arr ) // for each row in the array
        for( auto& value : row ) // for each value in the row
            value += 1.1 ; // add 1.1 to the value
    print() ;


    for( auto& row : arr ) // for each row in the array
        for( auto& value : row ) value = -98.7 ; // assign -98.7 to the each value in the row
    print() ;
}

http://coliru.stacked-crooked.com/a/38a830d3d88e0473


> how to both use the array index to refer to the individual elements as well as show what those elements are.

If the array index must be used within the loop body, it is simpler to write it as a classic for loop.

However, for understanding how the range-based loop works, this may help:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

int main()
{
    double arr[3][4] = { { 1.2, 2.3, 3.4, 4.6 }, { 5.7, 6.7, 7.8, 8.9 }, { 9.1, 8.2, 7.3, 6.4 } };

    std::size_t row_num = 0 ;
    for( const auto& row : arr )
    {
        std::size_t col_num = 0 ;
        for( const auto& value : row )
        {
            std::cout << "arr[" << row_num << "][" << col_num << "] == " << value << "    " ;
            
            ++col_num ; // increment the column number after each value is processed
        }

        ++row_num ; // increment the row number after each row is done
        std::cout << '\n' ;
    }
}

http://coliru.stacked-crooked.com/a/c2cb51bfdb25c0f1
Topic archived. No new replies allowed.