将一行二维数组分配给一维数组
Assign one row of 2d array to 1d array
我想将一行二维数组分配给一维数组,这就是我想做的。 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
。
相关文章:
- 如何正确填充在堆上分配的二维数组?
- 在 c++ 中分配布尔值的动态二维数组时超出内存限制
- 尝试从动态分配的二维数组 C++ 中读取值时出现分段错误,并在尝试删除它时给出 munmap_chunk():
- 在 C++ 中交叉动态分配的二维数组时的侵入性值/地址
- 如何将 50 行和 50 列的二维数组编码为一个为元素随机分配星号的函数?
- 程序终止,状态为 -1073741510 在 for 循环的中间,将输入分配给二维数组
- 为什么这个指针到指针的二维数组像这样分配新的 int
- 如何在C++中动态分配连续的二维数组?
- C 从嵌套循环中的文件中读取,并将值分配给二维数组
- 动态分配缓冲空间为二维数组 - 为什么失败
- 二维数组中的动态内存分配
- 从命令行输入分配二维数组
- 如何分配两个二维数组
- 二维数组分配中的内存管理
- 二维数组和分配对象
- 二维数组C++的动态分配
- 正确分配和取消分配指向对象的指针的二维数组
- 如何在C++中动态分配全局二维数组
- 如何在不分配内存的情况下将一维数组转换为二维数组
- 二维数组分配中的c++分割错误