Reputation: 5444
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?
Upvotes: 0
Views: 1292
Reputation: 3795
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!
Upvotes: 2
Reputation: 5444
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
Upvotes: 0
Reputation: 310980
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
Upvotes: 2