将一行二维数组分配给一维数组

Assign one row of 2d array to 1d array

本文关键字:二维数组 分配 一维数组 一行      更新时间:2023-10-16

我想将一行二维数组分配给一维数组,这就是我想做的。 int words[40][16]; int arrtemp[16]; arrtemp=words[i];

使用 std::copy:

int words[40][16]; //Be sure to initialise words and i at some point
int arrtemp[16];
//If you don't have std::begin and std::end,
//use boost::begin and boost::end (from Boost.Range) or
//std::copy(words[i] + 0, words[i] + 16, arrtemp + 0) instead.
std::copy(std::begin(words[i]), std::end(words[i]), std::begin(arrtemp));
数组

在C和C++中是不可变的。您无法重新分配它们。

您可以使用memcpy

memcpy(arrtemp, words[i], 16 * sizeof(int) );

这会将 16 * sizeof(int) 个字节从 words[i] 复制到 arrtemp