0

I have following struct:

 struct nodo {
 array<array<bool,19>,19> tablero; 
 array<int,2> p1,p2;     
 int d1,d2;                             
 int u1,u2;                             
 } mys;

which is passed by argument to a function

void myf(nodo mys){
// that attempts to do the following conversion:
array<array<int,19>,19> lab=mys.tablero;
}

And I am receiving the following error:

error: no match for ‘operator=’ in ‘lab = mys.nodo::tablero’

So I assume the conversion cannot be done like that. What is the most efficient way to do it?

6
  • 2
    Two for loops would do the trick. Commented Apr 7, 2015 at 16:39
  • What do you need to do with that bool array? Also why do you need it to be an int array in the first place? Commented Apr 7, 2015 at 16:40
  • @Mark Ransom Is that the most efficient way to do it? Commented Apr 7, 2015 at 16:42
  • 1
    Uh, not a duplicate, array has no such constructor. Commented Apr 7, 2015 at 16:42
  • Does efficiency really matter in this case? There's nothing straight-forward which will be any faster anyway. Commented Apr 7, 2015 at 16:54

3 Answers 3

2

These two arrays

array<array<bool,19>,19> tablero

and

array<array<int,19>,19> lab;

have different types and there is no implicit conversion from one array to another.

You can either write loops yourself or use some standard algorithms as it is shown in this demonstrative program

#include <iostream>
#include <array>
#include <algorithm>
#include <numeric>

int main() 
{

    std::array<std::array<bool,19>,19> tablero; 
    std::array<std::array<int,19>,19> tablero1;

    std::accumulate( tablero.begin(), tablero.end(), tablero1.begin(),
                     []( auto it, const auto &a ) 
                     { 
                        return std::copy( a.begin(), a.end(), it->begin() ), ++it;
                     } );

    return 0;
}

Your compiler has to support specifier auto in lambda expressions that the code would compile.

Or the same program but with some output

#include <iostream>
#include <iomanip>
#include <array>
#include <algorithm>
#include <numeric>

int main() 
{
    const size_t N = 3;
    std::array<std::array<bool, N>, N> tablero = 
    {
        {
            { true, false, false },
            { false, true, false },
            { false, false, true }
        }
    }; 
    std::array<std::array<int, N>, N> tablero1;

    std::accumulate( tablero.begin(), tablero.end(), tablero1.begin(),
                     []( auto it, const auto &a ) 
                     { 
                        return std::copy( a.begin(), a.end(),it->begin() ), ++it;
                     } );

    for ( const auto &a : tablero )
    {
        for ( auto b : a ) std::cout << std::boolalpha << b << ' ';
        std::cout << std::endl;
    }

    std::cout << std::endl;

    for ( const auto &a : tablero1 )
    {
        for ( auto x : a ) std::cout << x << ' ';
        std::cout << std::endl;
    }

    return 0;
}


true false false 
false true false 
false false true 

1 0 0 
0 1 0 
0 0 1 
Sign up to request clarification or add additional context in comments.

Comments

2

Here is how you can assign a 2D boolean array to a 2D integer array (of size 19x19 as in your case):

for(int i=0; i<19; i++) {
    for(int j=0; j<19; j++) {
       lab[i][j] = (tablero[i][j])?1:0;
    }
}

Please note the use of ternary operator in the assignment statement. If the

tablero[i][j] 

is true then

lab[i][j] will be 1, otherwise it will be 0.

Of course you may assign different integer values as well.

Hope that helps!

Comments

0

Well, so the most straightforward method is simply:

  for (i=0; i<=18; i++){
        for (j=0; j<=18 ; j++){
                lab[i][j]= mys.tablero[i][j];
            }
 }

As Mark Ransom suggested in the first place

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.