【问题标题】:Converting array of bool to array of int将 bool 数组转换为 int 数组
【发布时间】:2015-06-12 08:17:53
【问题描述】:

我有以下结构:

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

通过参数传递给函数

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

我收到以下错误:

错误:“lab = mys.nodo::tablero”中的“operator=”不匹配

所以我认为不能像那样进行转换。最有效的方法是什么?

【问题讨论】:

  • 两个 for 循环可以解决问题。
  • bool 数组需要做什么?另外,为什么你首先需要它是一个 int 数组?
  • @Mark Ransom 这是最有效的方法吗?
  • 呃,不是重复的,array 没有这样的构造函数。
  • 在这种情况下效率真的很重要吗?无论如何,没有什么比这更快了。

标签: c++


【解决方案1】:

这两个数组

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

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

具有不同的类型,并且没有从一个数组到另一个数组的隐式转换。

您可以自己编写循环,也可以使用本演示程序中显示的一些标准算法

#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;
}

您的编译器必须支持代码将编译的 lambda 表达式中的说明符 auto。

或相同的程序,但有一些输出

#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 

【讨论】:

    【解决方案2】:

    以下是如何将 2D 布尔数组分配给 2D 整数数组(大小为 19x19,如您的情况):

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

    请注意在赋值语句中使用三元运算符。如果

    tablero[i][j] 
    

    那么是真的

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

    当然你也可以分配不同的整数值。

    希望有帮助!

    【讨论】:

      【解决方案3】:

      嗯,所以最直接的方法就是:

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

      正如 Mark Ransom 一开始建议的那样

      【讨论】:

        猜你喜欢
        • 2011-05-25
        • 1970-01-01
        • 2015-12-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-09
        • 2011-04-06
        • 1970-01-01
        相关资源
        最近更新 更多