是否可以将两个多维数组一起添加到第三个多维数组中

Is it possible to add two multi-dimensional arrays together to a third multi-dimensional array

本文关键字:数组 三个 添加 两个 是否 一起      更新时间:2023-10-16

我试图将两个多维数组一起添加到第三个数组中,但没有成功。我创建的前两个数组都有值,我不确定如何将多维数组a添加到多维数组b中,并将值正确地放在多向数组c中。以下是我开始想到的。

提前感谢您的时间和技能。

int main()
{
int a[2] [3] = 
{
    { 16, 18, 23 },
    { 54, 91, 11 }
};
int b[2][3] =
{
        { 14, 52, 77 },
        { 16, 19, 59 }
};
int c[2][3];
for (int rows = 0; rows < 2; rows++)
{
    for (int columns = 0; columns < 3; columns++)
    {
        c[rows][columns] = b[rows][columns] + a[rows][columns];
    }
}
_getch();
return 0;
}

缩短指针长度的一种方法是:压平指针并使用transform

int c[2][3];
std::transform( *a, *std::end(a), *b, *c, std::plus<int>() ); // Or plus<> since C++14