0

我有一个 2d 数组网格作为 int GRID[10][20]; 我想要做的是删除最后一行 GRID[LAST][ALL] 并在数组的开头插入一个空白行。我试过用谷歌搜索这个没有运气。谢谢

4

2 回答 2

3

这不是 C++,这是 C。你可以这样做:

memmove( GRID[1], GRID, sizeof GRID - sizeof GRID[0] ); // shift the array
bzero( GRID, sizeof GRID[0] ); // zero out the first row

如果你使用 C++,它看起来像这样:

GRID.pop_back(); // remove the last row
GRID.push_front( std::vector< int >( 10 ) ); // add an initial row

或者这个(避免分配内存和对大小参数的依赖):

rotate( GRID.begin(), GRID.end()-1, GRID.end() ); // shift the array
fill( GRID[0].begin(), GRID[0].end(), 0 ); // zero out the first row

此外,在 C++ 中,您可以使用队列而不是向量,这正是您想要的。然而,在 C++ 中,多维容器(vectordeque)实际上是指向数组的指针数组,即不连续的内存结构,这与驻留在单个内存块中的 C 样式的数组数组不同。

于 2009-09-13T14:28:39.617 回答
1

数组是具有固定大小的静态结构。要获得您正在寻找的东西(具有插入和删除功能的可索引连续存储),您应该查看 STL 向量容器类型。

于 2009-09-13T14:08:10.320 回答